在Windows的多線程編程中,創建線程的函數主要有CreateThread和_beginthread(及_beginthreadex)。
CreateThread 和 ExitThread
使用API函數CreateThread創建線程時,其中的線程函數原型:
DWORD WINAPI ThreadProc(LPVOID lpParameter);
在線程函數返回后,其返回值用作調用ExitThread函數的參數(由系統隱式調用)。可以使用GetExitCodeThread函數獲得該線程函數的返回值。
當線程函數的起始地址無效(或者不可訪問)時,CreateThread函數仍可能成功返回。如果該起始地址無效,則當線程運行時,異常將發生,線程終止。並返回一個錯誤代碼。
使用CreateThread創建的線程具有THREAD_PRIORITY_NORMAL的線程優先級。可以使用GetThreadPriority和SetThreadPriority函數獲取和設置線程優先級值。
系統中的線程對象一直存活到線程結束,並且所有指向它的句柄都通過調用CloseHandle關閉后。
_beginthread 和 _endthread (_beginthread & _endthread)
對於使用C運行時庫里的函數的線程應該使用_beginthread和_endthread這些C運行時函數來管理線程,而不是使用CreateThread和ExitThread。否則,當調用ExitThread后,可能引發內存泄露。
在使用_beginthread或者_beginthreadex創建線程時,應該包含頭文件<process.h>,並且需要設置多線程版 本的運行時庫。「Project Settings」--> 「C/C++」-->「Category」-->「Code Generation」-->「Use Run-Time Library」-->「Multithreaded」和「Debug Multithreaded」。這相當於給編譯添加了一個編譯選項/MT,使編譯器在編譯時在.obj文件中使用libcmt.lib文件名而不是 libc.lib。連接器使用這個名字與運行時庫函數連接。
可以調用_endthread和_endthreadex顯示式結束一個線程。然而,當線程函數返回時,_endthread和_endthreadex 被自動調用。endthread和_endthreadex的調用有助於確保分配給線程的資源的合理回收。_endthread自動地關閉線程句柄,然而 _endthreadex卻不會。因此,當使用_beginthread和_endthread時,不需要顯示調用API函數CloseHandle式關 閉線程句柄。該行為有別於API函數ExitThread的調用。_endthread和_endthreadex回收分配的線程資源后,調用 ExitThread。
當_beginthread和_beginthreadex被調用時,操作系統自己處理線程棧的分配。如果在調用這些函數時,指定棧大小為0,則操作系統 為該線程創建和主線程大小一樣的棧。如果任何一個線程調用了abort、exit或者ExitProcess,則所有線程都將被終止。
線程是操作系統管理的一種資源,不同操作系統差異很大,有的支持,有的不支持,實現的方式也不同,下面是引用的LINUX下多線程例子,使用pthread庫(第三方庫),簡單說明下:
/*thread_example.c : c multiple thread programming in linux
*author : falcon
*date : 2006.8.15
*e-mail : [email]tunzhj03@st.lzu.edu.cn[/email]
*/
#include <pthread.h>
#include <stdio.h>
#include <sys/time.h>
#define MAX 10
pthread_t thread[2]; //創建線程函數返回類型
pthread_mutex_t mut; //互斥鎖類型
int number=0, i;
void *thread1() //線程函數
{
printf ("thread1 : I'm thread 1\n");
for (i = 0; i < MAX; i++)
{
printf("thread1 : number = %d\n",number);
pthread_mutex_lock(&mut); //加鎖,用於對共享變量操作
number++;
pthread_mutex_unlock(&mut); //解鎖
sleep(2);
}
printf("thread1 :主函數在等我完成任務嗎?\n");
pthread_exit(NULL);
}
void *thread2()
{
printf("thread2 : I'm thread 2\n");
for (i = 0; i < MAX; i++)
{
printf("thread2 : number = %d\n",number);
pthread_mutex_lock(&mut);
number++;
pthread_mutex_unlock(&mut);
sleep(3);
}
printf("thread2 :主函數在等我完成任務嗎?\n");
pthread_exit(NULL);
}
void thread_create(void)
{
/*創建線程*/
pthread_create(&thread[0], NULL, thread1, NULL);
printf("線程1被創建\n");
pthread_create(&thread[1], NULL, thread2, NULL);
printf("線程2被創建\n");
}
void thread_wait(void)
{
/*等待線程結束*/
pthread_join(thread[0],NULL);
printf("線程1已經結束\n");
pthread_join(thread[1],NULL);
printf("線程2已經結束\n");
}
int main()
{
/*用默認屬性初始化互斥鎖*/
pthread_mutex_init(&mut,NULL);
printf("我是主函數哦,我正在創建線程,呵呵\n");
thread_create();
printf("我是主函數哦,我正在等待線程完成任務阿,呵呵\n");
thread_wait();
return 0;
}
pthread相關:
運行之前需要做一些配置:
1.下載PTHREAD的WINDOWS開發包 pthreads-w32-2-4-0-release.exe(任何一個版本均可)
http://sourceware.org/pthreads-win32/ ,解壓到一個目錄。
2.找到include和lib文件夾,下面分別把它們添加到VC++6.0的頭文件路徑和靜態鏈接庫路徑下面:
a).Tools->Options,選擇Directory頁面,然后在Show directories for:中選擇Include files(默認) 在Directories中添加include的路徑。在Show directories for:中選擇Library files,
在Directories中添加lib的路徑。
b).Project->Settings,選擇Link頁面,然后將lib下的*.lib文件添加到Object/library Modules,
各lib文件以空格隔開。
c).將lib下的*.dll文件復制到工程目錄下,即根目錄。
3.代碼
1.#include <stdio.h>
2.#include <stdlib.h>
3.#include <pthread.h>
4.#include <windows.h>
5.
6.int piao = 100;
7.
8.pthread_mutex_t mut;
9.
10.void* tprocess1(void* args){
11. int a = 0;
12. while(true){
13. pthread_mutex_lock(&mut);
14. if(piao>0){
15. Sleep(1);
16. piao--;
17. printf("窗口1----------------還剩%d張票\n",piao);
18. }else{
19. a = 1;
20. }
21. pthread_mutex_unlock(&mut);
22. if(a == 1) {
23. break;
24. }
25. }
26.
27.
28. return NULL;
29.}
30.
31.void* tprocess2(void* args){
32. int a = 0;
33. while(true){
34. pthread_mutex_lock(&mut);
35. if(piao>0){
36. Sleep(1);
37. piao--;
38. printf("窗口2----------------還剩%d張票\n",piao);
39. }else{
40. a = 1;
41. }
42. pthread_mutex_unlock(&mut);
43. if(a == 1) {
44. break;
45. }
46. }
47.
48.
49. return NULL;
50.}
51.
52.void* tprocess3(void* args){
53. int a = 0;
54. while(true){
55. pthread_mutex_lock(&mut);
56. if(piao>0){
57. Sleep(1);
58. piao--;
59.
60. printf("窗口3----------------還剩%d張票\n",piao);
61. }else{
62. a = 1;
63. }
64. pthread_mutex_unlock(&mut);
65. if(a == 1) {
66. break;
67. }
68. }
69.
70.
71. return NULL;
72.}
73.
74.void* tprocess4(void* args){
75. int a = 0;
76. while(true){
77. pthread_mutex_lock(&mut);
78. if(piao>0){
79. Sleep(1);
80.
81. piao--;
82.
83. printf("窗口4----------------還剩%d張票\n",piao);
84. }else{
85. a = 1;
86. }
87. pthread_mutex_unlock(&mut);
88. if(a == 1) {
89. break;
90. }
91. }
92.
93.
94. return NULL;
95.}
96.
97.int main(){
98. pthread_mutex_init(&mut,NULL);
99. pthread_t t1;
100. pthread_t t2;
101. pthread_t t3;
102. pthread_t t4;
103. pthread_create(&t4,NULL,tprocess4,NULL);
104. pthread_create(&t1,NULL,tprocess1,NULL);
105. pthread_create(&t2,NULL,tprocess2,NULL);
106. pthread_create(&t3,NULL,tprocess3,NULL);
107. Sleep(5000);
108. return 0;
109.}

