beginthreadex建立多執行緒的用法

2021-09-29 05:59:47 字數 1576 閱讀 7672

#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) function or a static

// class member function as the thread entry-point-function.

// furthermore, _beginthreadex() demands that the thread

// entry function signature take a single (void*) and returned

// an unsigned.

static unsigned __stdcall threadstaticentrypoint(void * pthis)

void threadentrypoint()

}printf( "%s thread terminating\n", threadname.c_str() );

}};int main()

1.如果你正在編寫c/c++**,決不應該呼叫createthread。相反,應該使用visualc++執行期庫函式_beginthreadex,推出也應該使用_endthreadex。如果不使用microsoft的visualc++編譯器,你的編譯器**商有它自己的createthred替代函式。不管這個替代函式是什麼,你都必須使用。

2.因為_beginthreadex和_endthreadex是crt執行緒函式,所以必須注意編譯選項runtimelibaray的選擇,使用mt或mtd。

3._beginthreadex函式的引數列表與createthread函式的引數列表是相同的,但是引數名和型別並不完全相同。這是因為microsoft的c/c++執行期庫的開發小組認為,c/c++執行期函式不應該對windows資料型別有任何依賴。_beginthreadex函式也像createthread那樣,返回新建立的執行緒的控制代碼。

下面是關於_beginthreadex的一些要點:

4._endthreadex的一些要點:

5.雖然也提供了簡化版的的_beginthread和_endthread,但是可控制性太差,所以一般不使用。

6.執行緒handle因為是核心物件,所以需要在最後closehandle。

c++主線程的終止,同時也會終止所有主線程建立的子執行緒,不管子執行緒有沒有執行完畢。所以上面的**中如果不呼叫waitforsingleobject,則2個子執行緒t1和t2可能並沒有執行完畢或根本沒有執行。

如果某執行緒掛起,然後有呼叫waitforsingleobject等待該執行緒,就會導致死鎖。所以上面的**如果不呼叫resumethread,則會死鎖。

詳解 beginthreadex 建立執行緒

目錄 需要的標頭檔案支援 include for beginthread 需要的設定 project setting c c user run time library選擇debug multithreaded或者multithreaded。即使用 mt或mtd。如下 include include...

beginThreadex建立多執行緒解讀

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

建立主鍵 建立外來鍵 建立約束

建立主鍵 三種方法 建立學生表 第一種 create table student sno char 5 primary key,學號 可以直接指定主鍵 sname char 20 not null,姓名 s char 3 not null,性別 sage integer not null,年齡 sd...