多執行緒程式設計例項

2021-06-28 12:23:43 字數 1073 閱讀 8422

1、建立乙個執行緒用createthread函式

引數1:指向security_attribute結構體的乙個指標,再次可以設定為null,預設的安全性

引數2:指定初始提交的棧的大小,以位元組為單位。可以設定為0,使用預設大小。

引數3:指向乙個lspthread_start_routing型別的應用程式定義個函式的指標,該函式將被執行緒指向,表示了執行緒的起始位址。threadproc函式,函式名可以改變。

引數4:指定乙個單獨的引數的值,傳遞給執行緒函式threadproc。

引數5:指定控制線程建立的附加標記,如果create_suspended被指定,執行緒建立後處於暫停狀態,直到呼叫resumethread函式。如果該值為0,執行緒建立後立即執行。

引數6:返回值引數,用於接收執行緒的識別符號。在系統(nt/2000)下可以設定為null。

2、多執行緒演示火車票售票系統,包含互斥物件的使用

#include #include #include using namespace std;

dword winapi threadproc1(lpvoid lpparameter);//執行緒函式定義

dword winapi threadproc2(lpvoid lpparameter);//執行緒函式定義

int index = 0;

int tickets = 100;

handle hmutex;

int _tmain(int argc, _tchar* argv)

dword winapi threadproc1(lpvoid lpparameter)//執行緒函式實現

else

releasemutex(hmutex);//釋放指定互斥物件的所有權,是互斥物件變為有訊號狀態。

} return 0;

}dword winapi threadproc2(lpvoid lpparameter)//執行緒函式實現

else

releasemutex(hmutex);//釋放指定互斥物件的所有權,是互斥物件變為有訊號狀態。

} return 0;

}

多執行緒程式設計例項

有四個執行緒1 2 3 4。執行緒 1的功能就是輸出 1,執行緒 2的功能就是輸出 2,以此類推 現在有四個檔案 abcd 初始都為空。現要讓四個檔案呈如下格式 a 1 2 3 4 1 2.b 2 3 4 1 2 3.c 3 4 1 2 3 4.d 4 1 2 3 4 1.程式 include in...

多執行緒程式設計例項

include include include include include using namespace std intgetsum vector int iterator first,vector int iterator last static string getcurrenttime ...

C 多執行緒程式設計簡單例項

using system using system.collections using system.collections.generic using system.threading 在開發中經常會遇到執行緒的例子,如果某個後台操作比較費時間,我們就可以啟動乙個執行緒去執行那個費時的操作,同時程...