C語言中多線程編程包括的文件:#include<pthread.h>(linux環境下)
pthread_t //線程函數返回類型
pthread_mutrex_t //互斥鎖類型
int pthread_create(pthread_t *thread,pthread_attr_t *attr,void *(*func),void *arg);
參數說明
thread :指向thread_t類型變量的指針,用於保存線程的ID
typedef unsigned long int thread_t;
attr :指向pthread_attr_t類型變量的指針,或者為NULL
func:指向新線程所運行函數的指針
arg: 傳遞給func的參數
成功創建線程則返回0,否則返回非零
這個參數為線程屬性,pthread_attr_t主要包括:scope屬性,detach屬性,堆棧地址,堆棧大小,優先級。參數設置為NULL則將采用默認的屬性配置。
http://blog.csdn.net/hudashi/article/details/7709413
http://hi.baidu.com/7828058/blog/item/256e16decd1a385e94ee3784.html
當線程的屬性沒有設置為PTHREAD_CREATE_DETACH時候,我們可以使用pthread_join等待進程結束,來釋放資源。
函數原型
int pthread_join(pthread_t thread,void** retval)
參數說明:
thread:所等待的進程
retval:z指向某存儲線程返回值的變量
返回值:0 成功返回
errorcode 錯誤
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 { 12 int a = 0; 13 while (true) 14 { 15 pthread_mutex_lock(&mut); 16 if (piao>0) 17 { 18 Sleep(1); 19 piao--; 20 printf("窗口1----------------還剩%d張票\n", piao); 21 } 22 else 23 { 24 a = 1; 25 } 26 pthread_mutex_unlock(&mut); 27 if (a == 1) 28 { 29 break; 30 } 31 } 32 return NULL; 33 } 34 35 void* tprocess2(void* args) 36 { 37 int a = 0; 38 while (true) 39 { 40 pthread_mutex_lock(&mut); 41 if (piao>0) 42 { 43 Sleep(1); 44 piao--; 45 printf("窗口2----------------還剩%d張票\n", piao); 46 } 47 else 48 { 49 a = 1; 50 } 51 pthread_mutex_unlock(&mut); 52 if (a == 1) 53 { 54 break; 55 } 56 } 57 return NULL; 58 } 59 void* tprocess3(void* args) 60 { 61 int a = 0; 62 while (true) 63 { 64 pthread_mutex_lock(&mut); 65 if (piao>0) 66 { 67 Sleep(1); 68 piao--; 69 printf("窗口3----------------還剩%d張票\n", piao); 70 } 71 else 72 { 73 a = 1; 74 } 75 pthread_mutex_unlock(&mut); 76 if (a == 1) 77 { 78 break; 79 } 80 } 81 return NULL; 82 } 83 void* tprocess4(void* args) 84 { 85 int a = 0; 86 while (true) 87 { 88 pthread_mutex_lock(&mut); 89 if (piao>0) 90 { 91 Sleep(1); 92 piao--; 93 printf("窗口4----------------還剩%d張票\n", piao); 94 } 95 else 96 { 97 a = 1; 98 } 99 pthread_mutex_unlock(&mut); 100 if (a == 1) 101 { 102 break; 103 } 104 } 105 return NULL; 106 } 107 108 int main() { 109 pthread_mutex_init(&mut, NULL); 110 pthread_t t1; 111 pthread_t t2; 112 pthread_t t3; 113 pthread_t t4; 114 pthread_create(&t4, NULL, tprocess4, NULL); 115 pthread_create(&t1, NULL, tprocess1, NULL); 116 pthread_create(&t2, NULL, tprocess2, NULL); 117 pthread_create(&t3, NULL, tprocess3, NULL); 118 Sleep(5000); 119 return 0; 120 }
//線程一的線程函數一結束就自動釋放資源,線程二就得等到pthread_join來釋放系統資源。
#include <stdlib.h> #include <stdio.h> #include <errno.h> #include <pthread.h> static void pthread_func_1 (void); static void pthread_func_2 (void); int main (int argc, char** argv) { pthread_t pt_1 = 0; pthread_t pt_2 = 0; pthread_attr_t atrr = {0}; int ret = 0; /*初始化屬性線程屬性*/ pthread_attr_init (&attr); pthread_attr_setscope (&attr, PTHREAD_SCOPE_SYSTEM); pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED); ret = pthread_create (&pt_1, &attr, pthread_func_1, NULL); if (ret != 0) { perror ("pthread_1_create"); } ret = pthread_create (&pt_2, NULL, pthread_func_2, NULL); if (ret != 0) { perror ("pthread_2_create"); } pthread_join (pt_2, NULL); return 0; } static void pthread_func_1 (void) { int i = 0; for (; i < 6; i++) { printf ("This is pthread_1.\n"); if (i == 2) { pthread_exit (0); } } return; } static void pthread_func_2 (void) { int i = 0; for (; i < 3; i ++) { printf ("This is pthread_2.\n"); } return; }