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; }