日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Win32 多线程的创建方法,区别和联系

發布時間:2025/4/16 编程问答 22 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Win32 多线程的创建方法,区别和联系 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

2019獨角獸企業重金招聘Python工程師標準>>>

Win32多線程的創建方法主要有:

  • CreateThread()

  • _beginthread()&&_beginthreadex()

  • AfxBeginThread()

  • CWinThread類

  • 一、簡介

    CreateThread:?Win32提供的創建線程的最基礎的API,用于在主線程上創建一個線程。返回一個HANDLE句柄(內核對象)。在內核對象使用完畢后,一般需要關閉,使用CloseHandle()函數。

    _beginthread()&&_beginthreadex():

    在MSDN中可以看到一句很重要的提示,內容為“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的可執行程序,不要使用Win32的線程退出函數(ExitThread),這會阻止運行時系統回收分配的資源,應該使用_endthread,它能回收分配的線程資源然后調用ExitThread。這個問題看似沒有提到CreateThread(),但是其實有關,這就是經常看到有些資料上堅決的說到”不要使用CreateThread創建線程,否則會內存泄漏“的來源了。

    更詳細的介紹見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.

    簡單翻譯為: 在調用GetExitCodeThread之前,可能新創建的線程已經執行,返回并終止了,這個時候,hThread將無效,_endthread已經關掉了線程句柄。

    _beginthreadex >?_beginthread >?CreateThread

    AfxBeginThread:這是MFC中的Afx系列函數,一個在MFC中創建線程的全局函數。

    封裝了_beginthreadex,因此,MFC程序,盡量用該函數。

    CWinThread:UI線程,能接收消息,需要調用AfxBeginThread創建線程。

    AfxBeginThread(RUNTIME_CLASS(MyThread))

    二、部分參數介紹

    dwStackSize:線程堆棧大小,使用0采用默認設置,默認為1024K,所以默認只能創建不到2048個線程(2G內存).windows會根據需要動態增加堆棧大小。

    lpThreadAttributes:線程屬性。

    lpStartAddress:指向線程函數的指針。

    lpParameter:向線程函數傳遞的參數。

    dwCreationFlags:線程標志,CREATE_SUSPENDED表示創建一個掛起的線程,0表示創建后立即激活線程。

    lpThreadId,先線程的ID(輸出參數)


    轉載于:https://my.oschina.net/shanlilaideyu/blog/481536

    《新程序員》:云原生和全面數字化實踐50位技術專家共同創作,文字、視頻、音頻交互閱讀

    總結

    以上是生活随笔為你收集整理的Win32 多线程的创建方法,区别和联系的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。