• <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)  編輯 收藏 引用 所屬分類: 多線程
            亚洲国产日韩综合久久精品| 青青青国产精品国产精品久久久久| 久久久久久久久66精品片| 中文字幕热久久久久久久| 久久国产高清字幕中文| 久久久这里有精品| 国产激情久久久久影院小草| 亚洲日本va中文字幕久久| 中文字幕亚洲综合久久2| 久久婷婷五月综合97色一本一本| 久久精品成人欧美大片| 93精91精品国产综合久久香蕉| 亚洲色大成网站www久久九 | 亚洲国产精品无码久久久久久曰| 久久精品国产亚洲精品2020| 久久亚洲精品国产亚洲老地址| 国产高潮国产高潮久久久91| 久久精品国产精品亚洲毛片| 久久精品国产亚洲av麻豆图片 | 久久se精品一区二区影院| 好久久免费视频高清| 人妻少妇久久中文字幕| 精品国产乱码久久久久久呢| 热久久最新网站获取| 亚洲欧洲精品成人久久曰影片| 国产成人久久精品麻豆一区| 日本精品久久久久中文字幕| 色综合色天天久久婷婷基地| 色综合久久88色综合天天| 久久99精品国产| 亚洲一区中文字幕久久| 精品国产乱码久久久久久浪潮| 99久久精品久久久久久清纯| 很黄很污的网站久久mimi色| 久久精品国产亚洲Aⅴ蜜臀色欲| 久久成人精品| 欧美日韩精品久久久久| 久久久国产打桩机| 97久久天天综合色天天综合色hd | 久久99国产精品成人欧美| 欧美激情精品久久久久久久九九九|