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í)行。