亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Windows線程:_初學者與CreateThread C+

Windows線程:_初學者與CreateThread C+

C++
白板的微信 2019-10-13 12:08:29
Windows線程:_初學者與CreateThread C+有什么更好的方法來啟動一條線索,_beginthread, _beginthreadx或CreateThread?我正在試圖確定_beginthread, _beginthreadex和CreateThread..所有這些函數都返回一個新創建的線程句柄,我已經知道CreateThread在發生錯誤時提供了一些額外的信息(可以通過調用GetLastError).。但是當我使用這些函數時,我應該考慮什么呢?我正在使用一個windows應用程序,所以跨平臺兼容性已經不可能了。我已經閱讀了MSDN文檔,我只是不明白,例如,為什么會有人決定使用_start線程而不是CreateThread,反之亦然。干杯!更新:好的,謝謝你提供的所有信息,我也在幾個我不能打電話的地方讀到過WaitForSingleObject()如果我用_beginthread()但如果我打電話_endthread()在線上不應該起作用嗎?那是怎么回事?
查看完整描述

3 回答

?
www說

TA貢獻1775條經驗 獲得超8個贊

CreateThread()是一個原始的Win 32 API調用,用于在內核級別創建另一個控制線程。

_beginthread() & _beginthreadex()是調用C運行時庫的調用嗎?CreateThread()幕后。一次CreateThread()已經回來了,_beginthread/ex()負責額外的簿記,以使C運行時庫在新線程中可用&一致。

在C+中,您幾乎可以肯定地使用_beginthreadex()除非您根本不鏈接到C運行時庫(也就是MSVCRT*.dll/.lib)。



查看完整回答
反對 回復 2019-10-14
?
子衿沉夜

TA貢獻1828條經驗 獲得超3個贊

有幾個不同之處_beginthread()_beginthreadex()_beginthreadex()是為了表現得更像CreateThread()(這兩個參數以及它的行為方式)。

如果您使用的是C/C+運行時,則必須使用_beginthread()/_beginthreadex()而不是CreateThread()這樣,運行時就有機會執行自己的線程初始化(設置線程本地存儲,等等)。

實際上,這意味著CreateThread()應該不會被你的代碼直接使用。

的MSDN文檔_beginthread()/_beginthreadex()其中一個更重要的是,因為線程句柄是由_beginthread()當線程退出時,CRT自動關閉該線程,“如果_初學者線程生成的線程快速退出,則返回給_初學者線程調用方的句柄可能無效,或者更糟糕的是指向另一個線程”。

以下是評論的內容_beginthreadex()在CRT消息來源中必須指出:

Differences between _beginthread/_endthread and the "ex" versions:

1)  _beginthreadex takes the 3 extra parameters to CreateThread
  which are lacking in _beginthread():
    A) security descriptor for the new thread
    B) initial thread state (running/asleep)
    C) pointer to return ID of newly created thread

2)  The routine passed to _beginthread() must be __cdecl and has
  no return code, but the routine passed to _beginthreadex()
  must be __stdcall and returns a thread exit code.  _endthread
  likewise takes no parameter and calls ExitThread() with a
  parameter of zero, but _endthreadex() takes a parameter as
  thread exit code.

3)  _endthread implicitly closes the handle to the thread, but
  _endthreadex does not!

4)  _beginthread returns -1 for failure, _beginthreadex returns
  0 for failure (just like CreateThread).

為VS 2012執行的CRT有一個額外的初始化在_beginthreadex()*如果該過程是一個“打包應用程序”(如果從GetCurrentPackageId())運行時將在新創建的線程上初始化MTA。



查看完整回答
反對 回復 2019-10-14
?
守著星空守著你

TA貢獻1799條經驗 獲得超8個贊

一般來說,正確的做法是打電話給_beginthread()/_endthread()(或ex()變體)。但是,如果使用CRT作為.dll,則CRT狀態將被正確初始化,并作為CRT的狀態銷毀。DllMain將被調用DLL_THREAD_ATTACHDLL_THREAD_DETACH打電話時CreateThread()ExitThread()或者分別回來。

這個DllMainCRT的代碼可以在VC\CRT\src\crtlib.c下的VS安裝目錄中找到。


查看完整回答
反對 回復 2019-10-14
  • 3 回答
  • 0 關注
  • 406 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號