詳解 beginthreadex 建立執行緒

2022-09-20 18:33:09 字數 1510 閱讀 3451

目錄

需要的標頭檔案支援#include         // for _beginthread()

需要的設定:projectàsetting-->c/c++-->user run-time library選擇debug multithreaded或者multithreaded。即使用:mt或mtd。

**如下:

#include

#include // for stl string class

#include // for handle

#include // for _beginthread()

using namespace std;

class threadx

static unsigned __stdcall threadstaticentrypoint(void * pthis)

void threadentrypoint() }

printf( "%s thread terminating\n"程式設計客棧, threadname.c_str() );

}};int main()

注意:createthread是windows的api函式(sdk函式的標準形式,直截了當的建立方式,任何場合都可以使用),提供作業系統級別的建立執行緒的操作,且僅限於工作者執行緒。不呼叫mfc和rtl的函式時,可以用createthread,其它情況不要輕易。在使用的過程中要考慮到程序的同步與互斥的關係(防止死鎖)。執行緒函式定義為:dword winapi _yourthreadfun(lpvoid pparameter)。但它沒有考慮:

afxbeginthread:mfc中線程建立的mfc函式,首先建立了相應的cwinthread物件,然後呼叫cwinthread::createthread,   在cwinthread::createthread中,完成了對執行緒物件的初始化工作,然後,呼叫_beginthreadex(afxbeginthread相比較更為安全)建立執行緒。它簡化了操作或讓執行緒能夠響應訊息,即可用於介面執行緒,也可以用於工作者執行緒,但要注意不要在乙個mfc程式中使用_beginthreadex()或createthread()。執行緒函式定義為:uint

_yourthreadfun(lpvoid pparam)

_beginthreadex:ms對c runtime庫的擴充套件sdk函式,首先針對c runtime庫做了一些初始化的工作,以保證c runtime庫工作正常。然後,呼叫createthread真正建立執行緒。 僅使用runtime library時,可以用_begingthread。

_beginthreadex和_beginthread在**入口函式之前進行一些執行緒相關的crt的初始化操作。

crt的函式庫**程出現之前就已經存在,所以原有的crt不能真正支援執行緒,

這也導致了許多crt的函式在多執行緒的情況下必須有特殊的支援,不能簡單的使用createthread就ok。

補充一點:_beginthreadex()是針對crt的執行緒函式,**程中若要用到crt的函式,最好用這個啟動執行緒,如果不用這個會有記憶體洩漏。

beginThreadex建立多執行緒解讀

include for beginthread 需要的設定 project setting c c user run time library 選擇debug multithreaded 或者multithreaded。即使用 mt或mtd。原始碼如下 cpp view plain copy pri...

beginthreadex建立多執行緒的用法

include include for stl string class include for handle include for beginthread using namespace std class threadx in c you must employ a free c functi...

TCP IP詳解 卷1,2,3詳解

大家知道,tcp ip已成為計算機網路的事實上的標準。在關於tcp ip的論著中,最有影響的就是兩部著作。一部是douglas e.comer的 用tcp ip進行網際互連 而另一部就是stevens寫的這3卷書。這兩套巨著都很有名,各有其特點。無論是從事計算機網路的教師還是進行科研的技術人員,這兩...