• <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>
            2.創(chuàng)建線程

              進(jìn)程的主線程由操作系統(tǒng)自動生成,Win32提供了CreateThread API來完成用戶線程的創(chuàng)建,該API的原型為:

            HANDLE CreateThread(
             LPSECURITY_ATTRIBUTES lpThreadAttributes,//Pointer to a SECURITY_ATTRIBUTES structure
             SIZE_T dwStackSize, //Initial size of the stack, in bytes.
             LPTHREAD_START_ROUTINE lpStartAddress,
             LPVOID lpParameter, //Pointer to a variable to be passed to the thread
             DWORD dwCreationFlags, //Flags that control the creation of the thread
             LPDWORD lpThreadId //Pointer to a variable that receives the thread identifier
            );

              如果使用C/C++語言編寫多線程應(yīng)用程序,一定不能使用操作系統(tǒng)提供的CreateThread API,而應(yīng)該使用C/C++運(yùn)行時(shí)庫中的_beginthread(或_beginthreadex),其函數(shù)原型為:

            uintptr_t _beginthread(
             void( __cdecl *start_address )( void * ), //Start address of routine that begins execution of new thread
             unsigned stack_size, //Stack size for new thread or 0.
             void *arglist //Argument list to be passed to new thread or NULL
            );
            uintptr_t _beginthreadex(
             void *security,//Pointer to a SECURITY_ATTRIBUTES structure
             unsigned stack_size,
             unsigned ( __stdcall *start_address )( void * ),
             void *arglist,
             unsigned initflag,//Initial state of new thread (0 for running or CREATE_SUSPENDED for suspended);
             unsigned *thrdaddr
            );

              _beginthread函數(shù)與Win32 API 中的CreateThread函數(shù)類似,但有如下差異:

             ?。?)通過_beginthread函數(shù)我們可以利用其參數(shù)列表arglist將多個(gè)參數(shù)傳遞到線程;

             ?。?)_beginthread 函數(shù)初始化某些 C 運(yùn)行時(shí)庫變量,在線程中若需要使用 C 運(yùn)行時(shí)庫。

              3.終止線程

              線程的終止有如下四種方式:

              (1)線程函數(shù)返回;

             ?。?)線程自身調(diào)用ExitThread 函數(shù)即終止自己,其原型為:

            VOID ExitThread(UINT fuExitCode );

              它將參數(shù)fuExitCode設(shè)置為線程的退出碼。

              注意:如果使用C/C++編寫代碼,我們應(yīng)該使用C/C++運(yùn)行時(shí)庫函數(shù)_endthread (_endthreadex)終止線程,決不能使用ExitThread!
            _endthread 函數(shù)對于線程內(nèi)的條件終止很有用。例如,專門用于通信處理的線程若無法獲取對通信端口的控制,則會退出。

              (3)同一進(jìn)程或其他進(jìn)程的線程調(diào)用TerminateThread函數(shù),其原型為:

            BOOL TerminateThread(HANDLE hThread,DWORD dwExitCode);

              該函數(shù)用來結(jié)束由hThread參數(shù)指定的線程,并把dwExitCode設(shè)成該線程的退出碼。當(dāng)某個(gè)線程不再響應(yīng)時(shí),我們可以用其他線程調(diào)用該函數(shù)來終止這個(gè)不響應(yīng)的線程。

             ?。?)包含線程的進(jìn)程終止。

              最好使用第1種方式終止線程,第2~4種方式都不宜采用。

              4.掛起與恢復(fù)線程

              當(dāng)我們創(chuàng)建線程的時(shí)候,如果給其傳入CREATE_SUSPENDED標(biāo)志,則該線程創(chuàng)建后被掛起,我們應(yīng)使用ResumeThread恢復(fù)它:

            DWORD ResumeThread(HANDLE hThread);

              如果ResumeThread函數(shù)運(yùn)行成功,它將返回線程的前一個(gè)暫停計(jì)數(shù),否則返回0x FFFFFFFF。

              對于沒有被掛起的線程,程序員可以調(diào)用SuspendThread函數(shù)強(qiáng)行掛起之:

            DWORD SuspendThread(HANDLE hThread);

              一個(gè)線程可以被掛起多次。線程可以自行暫停運(yùn)行,但是不能自行恢復(fù)運(yùn)行。如果一個(gè)線程被掛起n次,則該線程也必須被恢復(fù)n次才可能得以執(zhí)行。
            Posted on 2006-11-18 14:20 艾凡赫 閱讀(675) 評論(0)  編輯 收藏 引用 所屬分類: 多線程
            日本WV一本一道久久香蕉| 亚洲国产一成人久久精品| 久久精品人人做人人爽电影| 国产欧美久久一区二区| 精品国产热久久久福利| 久久天天躁狠狠躁夜夜躁2014| 亚洲精品无码久久久久sm| 韩国三级大全久久网站| 久久丫忘忧草产品| 97精品伊人久久久大香线蕉| 久久香综合精品久久伊人| 91亚洲国产成人久久精品网址| 99久久精品免费看国产一区二区三区| 性色欲网站人妻丰满中文久久不卡 | 国产综合免费精品久久久| 亚洲日韩欧美一区久久久久我 | 久久丫精品国产亚洲av不卡| 国产L精品国产亚洲区久久 | 天天做夜夜做久久做狠狠| 国产成人精品久久二区二区| 亚洲精品综合久久| 品成人欧美大片久久国产欧美| 久久天天躁狠狠躁夜夜avapp| 欧美激情精品久久久久久久九九九| 国产精品对白刺激久久久| 一本一本久久aa综合精品| 亚洲成av人片不卡无码久久| 91精品观看91久久久久久| 久久99中文字幕久久| 国产V综合V亚洲欧美久久| 久久久av波多野一区二区| 久久人与动人物a级毛片| 亚洲人成无码www久久久| 国产精品乱码久久久久久软件| 欧美精品福利视频一区二区三区久久久精品 | 超级碰碰碰碰97久久久久| 久久久久99精品成人片牛牛影视 | 欧美亚洲国产精品久久久久| 人妻无码久久精品| 亚洲精品成人久久久| 久久人人爽人人爽人人av东京热|