• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>
            隨筆-5  評論-1  文章-0  trackbacks-0
            1.用事件對象來控制線程
            //Define thread function
            DWORD __stdcall ThreadFunOne(LPVOID lParam)
            {
                
            for(;;)
                {
                    WaitForSingleObject(hEvent, INFINITE);      
            //阻塞線程,直到事件對象為通知狀態
                    if (WorkerID<MAXWORKERID)
                    {

                        WorkerID 
            +=1;
                        Sleep(
            1000);
                        printf(
            "ThreadOne print out: %i \n",WorkerID);
                    }
                    SetEvent(hEvent);       
            //設置事件為通知狀態
                }    
                
            return 0;
            }
            DWORD __stdcall ThreadFunTwo(LPVOID lParam)
            {
                
            for(;;)
                {
                    WaitForSingleObject(hEvent, INFINITE);
                    
            if (WorkerID<MAXWORKERID)
                    {
                        WorkerID 
            +=1;
                        printf(
            "ThreadTwo print out: %i \n",WorkerID);
                        Sleep(
            1000);
                    }
                    SetEvent(hEvent);
                }
                
            return 0;
            }
            void main()
            {
                
            //Define thread handle
                HANDLE hThread1,hThread2;
                hEvent 
            = CreateEvent(NULL, FALSE, TRUE, "Event");
                
            //Create thread
                hThread1 = ::CreateThread(NULL,0,ThreadFunOne,NULL,0,NULL);
                hThread2 
            = ::CreateThread(NULL,0,ThreadFunTwo,NULL,0,NULL);
                
            //Close thread handle
                CloseHandle(hThread1);
                CloseHandle(hThread2);
                
            //Note: Prevent process exiting
                while (true)
                {
                    ;
                }
            }

            2.用臨界區來控制線程
            DWORD __stdcall ThreadFunOne(LPVOID lParam)
            {
                
            for(;;)
                {
                    EnterCriticalSection(
            &Section);
                    
            if (WorkerID<MAXWORKERID)
                    {

                        WorkerID 
            +=1;
                        Sleep(
            1000);
                        printf(
            "ThreadOne print out: %i \n",WorkerID);
                    }
                    LeaveCriticalSection(
            &Section);
                }
                
            return 0;
            }
            DWORD __stdcall ThreadFunTwo(LPVOID lParam)
            {
                
            for(;;)
                {
                    EnterCriticalSection(
            &Section);
                    
            if (WorkerID<MAXWORKERID)
                    {
                        WorkerID 
            +=1;
                        printf(
            "ThreadTwo print out: %i \n",WorkerID);
                        Sleep(
            1000);
                    }
                    LeaveCriticalSection(
            &Section);
                }
                
            return 0;
            }
            void main()
            {
                
            //Define thread handle
                HANDLE hThread1,hThread2;
                InitializeCriticalSection(
            &Section);
                
            //Create thread
                hThread1 = ::CreateThread(NULL,0,ThreadFunOne,NULL,0,NULL);
                hThread2 
            = ::CreateThread(NULL,0,ThreadFunTwo,NULL,0,NULL);
                
            //Close thread handle
                CloseHandle(hThread1);
                CloseHandle(hThread2);
                
            //Note: Prevent process exiting
                while (true)
                {
                    ;
                }
            }

            3.用互斥來控制線程
            DWORD __stdcall ThreadFunOne(LPVOID lParam)
            {
                
            for(;;)
                {
                    WaitForSingleObject(hMutex, INFINITE);
                    
            if (WorkerID<MAXWORKERID)
                    {

                        WorkerID 
            +=1;
                        Sleep(
            1000);
                        printf(
            "ThreadOne print out: %i \n",WorkerID);
                    }
                    ReleaseMutex(hMutex);
                }
                
            return 0;
            }
            DWORD __stdcall ThreadFunTwo(LPVOID lParam)
            {
                
            for(;;)
                {
                    WaitForSingleObject(hMutex, INFINITE);
                    
            if (WorkerID<MAXWORKERID)
                    {
                        WorkerID 
            +=1;
                        printf(
            "ThreadTwo print out: %i \n",WorkerID);
                        Sleep(
            1000);
                    }
                    ReleaseMutex(hMutex);
                }
                
            return 0;
            }
            void main()
            {
                
            //Define thread handle
                HANDLE hThread1,hThread2;
                hMutex 
            = CreateMutex(NULL, FALSE, "mutex");
                
            //Create thread
                hThread1 = ::CreateThread(NULL,0,ThreadFunOne,NULL,0,NULL);
                hThread2 
            = ::CreateThread(NULL,0,ThreadFunTwo,NULL,0,NULL);
                
            //Close thread handle
                CloseHandle(hThread1);
                CloseHandle(hThread2);
                
            //Note: Prevent process exiting
                while (true)
                {
                    ;
                }
            }
            posted on 2009-07-25 22:48 yunboy 閱讀(275) 評論(0)  編輯 收藏 引用 所屬分類: 進程與線程
            久久精品无码一区二区日韩AV| 久久国产乱子精品免费女| 99久久人妻无码精品系列| 久久精品国产第一区二区三区| 成人免费网站久久久| 亚洲综合精品香蕉久久网97| 国产精品永久久久久久久久久| 亚洲精品国产自在久久| 国产成人久久精品激情| 久久97久久97精品免视看秋霞 | 国产aⅴ激情无码久久| 国产精品久久久久久吹潮| 国内精品久久久久久中文字幕| 亚洲AV无码久久精品狠狠爱浪潮| 狠狠色丁香久久婷婷综合五月| 青青久久精品国产免费看| 青青青伊人色综合久久| 亚洲乱码中文字幕久久孕妇黑人| 国产一区二区精品久久岳| 久久婷婷国产综合精品| 久久影视国产亚洲| 青青青国产精品国产精品久久久久| 婷婷久久五月天| 久久久精品国产Sm最大网站| 国产精品国色综合久久| 亚洲女久久久噜噜噜熟女| 久久久无码精品亚洲日韩蜜臀浪潮| 国产成人久久精品麻豆一区| 国产美女久久精品香蕉69| 欧洲人妻丰满av无码久久不卡| 久久91精品国产91| 婷婷久久综合| 蜜臀久久99精品久久久久久| 大伊人青草狠狠久久| 精品国产乱码久久久久久郑州公司 | 99久久成人18免费网站| 97久久超碰国产精品2021| 人妻无码αv中文字幕久久琪琪布| 久久婷婷五月综合97色直播| 久久免费香蕉视频| 色狠狠久久综合网|