Win32 多线程的创建方法,区别和联系
2019獨(dú)角獸企業(yè)重金招聘Python工程師標(biāo)準(zhǔn)>>>
Win32多線程的創(chuàng)建方法主要有:
CreateThread()
_beginthread()&&_beginthreadex()
AfxBeginThread()
CWinThread類
一、簡介
CreateThread:?Win32提供的創(chuàng)建線程的最基礎(chǔ)的API,用于在主線程上創(chuàng)建一個線程。返回一個HANDLE句柄(內(nèi)核對象)。在內(nèi)核對象使用完畢后,一般需要關(guān)閉,使用CloseHandle()函數(shù)。
_beginthread()&&_beginthreadex():
在MSDN中可以看到一句很重要的提示,內(nèi)容為“For an executable file linked with Libcmt.lib, do not call the Win32 ExitThread API; this prevents the run-time system from reclaiming allocated resources. _endthread and _endthreadex reclaim allocated thread resources and then call ExitThread.”,簡單翻譯就是說,對于鏈接Libcmt.lib的可執(zhí)行程序,不要使用Win32的線程退出函數(shù)(ExitThread),這會阻止運(yùn)行時系統(tǒng)回收分配的資源,應(yīng)該使用_endthread,它能回收分配的線程資源然后調(diào)用ExitThread。這個問題看似沒有提到CreateThread(),但是其實(shí)有關(guān),這就是經(jīng)常看到有些資料上堅決的說到”不要使用CreateThread創(chuàng)建線程,否則會內(nèi)存泄漏“的來源了。
更詳細(xì)的介紹見http://wenku.baidu.com/view/adede4ec4afe04a1b071dea4.html
也就是說:盡量用_beginthread()而不是CreateThread
??Windows核心編程上如此說:??
These two functions were originally created to do the work of the new _beginthreadex and _endthreadex functions, respectively. However, as you can see, the _beginthread function has fewer parameters and is therefore more limited than the full-featured _beginthreadex function. For example, if you use _beginthread, you cannot create the new thread with security attributes, you cannot create the thread suspended, and you cannot obtain the thread's ID value. The _endthread function has a similar story: it takes no parameters, which means that the thread's exit code is hardcoded to 0.
_endthread還有個問題:
DWORD?dwExitCode; HANDLE?hThread?=?_beginthread(...); GetExitCodeThread(hThread,?&dwExitCode); CloseHandle(hThread);The newly created thread might execute, return, and terminate before the first thread can call GetExitCodeThread. If this happens, the value in hThread will be invalid because _endthread has closed the new thread's handle. Needless to say, the call to CloseHandle will also fail for the same reason.
簡單翻譯為: 在調(diào)用GetExitCodeThread之前,可能新創(chuàng)建的線程已經(jīng)執(zhí)行,返回并終止了,這個時候,hThread將無效,_endthread已經(jīng)關(guān)掉了線程句柄。
_beginthreadex >?_beginthread >?CreateThread
AfxBeginThread:這是MFC中的Afx系列函數(shù),一個在MFC中創(chuàng)建線程的全局函數(shù)。
封裝了_beginthreadex,因此,MFC程序,盡量用該函數(shù)。
CWinThread:UI線程,能接收消息,需要調(diào)用AfxBeginThread創(chuàng)建線程。
AfxBeginThread(RUNTIME_CLASS(MyThread))二、部分參數(shù)介紹
dwStackSize:線程堆棧大小,使用0采用默認(rèn)設(shè)置,默認(rèn)為1024K,所以默認(rèn)只能創(chuàng)建不到2048個線程(2G內(nèi)存).windows會根據(jù)需要動態(tài)增加堆棧大小。
lpThreadAttributes:線程屬性。
lpStartAddress:指向線程函數(shù)的指針。
lpParameter:向線程函數(shù)傳遞的參數(shù)。
dwCreationFlags:線程標(biāo)志,CREATE_SUSPENDED表示創(chuàng)建一個掛起的線程,0表示創(chuàng)建后立即激活線程。
lpThreadId,先線程的ID(輸出參數(shù))
轉(zhuǎn)載于:https://my.oschina.net/shanlilaideyu/blog/481536
《新程序員》:云原生和全面數(shù)字化實(shí)踐50位技術(shù)專家共同創(chuàng)作,文字、視頻、音頻交互閱讀總結(jié)
以上是生活随笔為你收集整理的Win32 多线程的创建方法,区别和联系的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: cmd常见命令
- 下一篇: 第十三章、facl及用户及Linux终端