linux下多线程编程

我只是一个虾纸丫 提交于 2020-03-27 23:01:10

先看执行的结果:

 1 主函数正在创建线程,...
 2 线程1被创建
 3 Thread1 : I'm thread 1th
 4 线程2被创建
 5 Thread2 : I'm thread 2nd
 6 thread2 : number = 0
 7 线程3被创建
 8 主函数正在等待线程结束...
 9 thread1 : number = 0
10 Thread3 : I'm thread 3nd
11 thread3 : number = 2
12 thread3 : number = 3
13 thread2 : number = 4
14 thread1 : number = 5
15 thread3 : number = 6
16 thread2 : number = 7
17 thread3 : number = 8
18 thread1 : number = 9
19 thread3 : number = 10
20 thread2 : number = 11
21 thread3 : number = 12
22 thread2 : number = 13
23 thread1 : number = 14
24 thread3 : number = 15
25 thread3 : number = 16
26 thread2 : number = 17
27 thread1 : number = 18
28 thread3 : number = 19
29 thread2 : number = 20
30 thread3 : number = 21
31 thread1 : number = 22
32 thread3 : number = 23
33 thread2 : number = 24
34 thread3 : number = 25
35 thread2 : number = 26
36 thread1 : number = 27
37 thread3 : number = 28
38 thread3 : number = 29
39 thread2 : number = 30
40 thread1 : number = 31
41 Thread3 :main函数在等我完成任务吗?
42 Thread2 :main函数在等我完成任务吗?
43 Thread1 :main函数在等我完成任务吗?
44 线程1已经结束
45 线程2已经结束
46 线程3已经结束

代码如下:参考网上的,稍微修改了下:否则,不能编译和运行:

  1 //fileName: threadSample.c
  2 #include <pthread.h>
  3 #include <stdio.h>
  4 #include <sys/time.h>
  5 #include <string.h>
  6 #include <unistd.h>                        //添加的头文件
  7 #define MAX 30
  8 
  9 pthread_t thread[3];  //3个线程
 10 pthread_mutex_t mut;
 11 int number=0;
 12 int i;
 13 
 14 void *thread1(){
 15         printf ("Thread1 : I'm thread 1th\n");
 16         for (i = 0; i < MAX; i++)
 17         {
 18                 printf("thread1 : number = %d\n",number);
 19                 pthread_mutex_lock(&mut);
 20                         number++;
 21                 pthread_mutex_unlock(&mut);
 22                 sleep(4);
 23         }
 24 
 25         printf("Thread1 :main函数在等我完成任务吗?\n");
 26         pthread_exit(NULL);
 27 }
 28 
 29 void *thread2(){
 30         printf("Thread2 : I'm thread 2nd\n");
 31 
 32         for (i = 0; i < MAX; i++)
 33         {
 34                 printf("thread2 : number = %d\n",number);
 35                 pthread_mutex_lock(&mut);
 36                         number++;
 37                 pthread_mutex_unlock(&mut);
 38                 sleep(3);
 39         }
 40 
 41         printf("Thread2 :main函数在等我完成任务吗?\n");
 42         pthread_exit(NULL);
 43 }
 44 
 45 void *thread3(){
 46         printf("Thread3 : I'm thread 3nd\n");
 47 
 48         for (i = 0; i < MAX; i++)
 49         {
 50                 printf("thread3 : number = %d\n",number);
 51                 pthread_mutex_lock(&mut);
 52                         number++;
 53                 pthread_mutex_unlock(&mut);
 54                 sleep(2);
 55         }
 56 
 57         printf("Thread3 :main函数在等我完成任务吗?\n");
 58         pthread_exit(NULL);
 59 }
 60 
 61 void thread_create(void){                                                                  int temp;
 62         memset(&thread, 0, sizeof(thread));
 63         /*创建线程*/
 64         if((temp = pthread_create(&thread[0], NULL, thread1, NULL)) != 0)
 65                 printf("线程1创建失败!\n");
 66         else
 67                 printf("线程1被创建\n");
 68 
 69         if((temp = pthread_create(&thread[1], NULL, thread2, NULL)) != 0)
 70                 printf("线程2创建失败");
 71         else
 72                 printf("线程2被创建\n");
 73 
 74         if((temp = pthread_create(&thread[2], NULL, thread3, NULL)) != 0)
 75                 printf("线程3创建失败");
 76         else
 77                 printf("线程3被创建\n");
 78 }
 79 
 80 void thread_wait(void){
 81         /*等待线程结束*/
 82         if(thread[0] !=0){
 83                 pthread_join(thread[0],NULL);
 84                 printf("线程1已经结束\n");
 85         }
 86         if(thread[1] !=0){
 87                pthread_join(thread[1],NULL);
 88                printf("线程2已经结束\n");
 89         }
 90         if(thread[2] !=0){
 91                pthread_join(thread[2],NULL);
 92                printf("线程3已经结束\n");
 93         }
 94 }
 95 
 96 int main()
 97 {
 98         /*用默认属性初始化互斥锁*/
 99         pthread_mutex_init(&mut,NULL);
100 
101         printf("主函数正在创建线程,...\n");
102         thread_create();
103         printf("主函数正在等待线程结束...\n");
104         thread_wait();
105 
106         return 0;
107 }

再次执行结果为:

 1 主函数正在创建线程,...
 2 线程1被创建
 3 Thread1 : I'm thread 1th
 4 thread1 : number = 0
 5 Thread2 : I'm thread 2nd
 6 thread2 : number = 1
 7 线程2被创建
 8 线程3被创建
 9 主函数正在等待线程结束...
10 Thread3 : I'm thread 3nd
11 thread3 : number = 2
12 thread3 : number = 3
13 thread2 : number = 4
14 thread1 : number = 5
15 thread3 : number = 6
16 thread2 : number = 7
17 thread3 : number = 8
18 thread1 : number = 9
19 thread3 : number = 10
20 thread2 : number = 11
21 thread3 : number = 12
22 thread1 : number = 13
23 thread2 : number = 14
24 thread3 : number = 15
25 thread3 : number = 16
26 thread2 : number = 17
27 thread1 : number = 18
28 thread3 : number = 19
29 thread2 : number = 20
30 thread3 : number = 21
31 thread1 : number = 22
32 thread3 : number = 23
33 thread2 : number = 24
34 thread3 : number = 25
35 thread1 : number = 26
36 thread2 : number = 27
37 thread3 : number = 28
38 thread3 : number = 29
39 thread2 : number = 30
40 thread1 : number = 31
41 Thread3 :main函数在等我完成任务吗?
42 Thread2 :main函数在等我完成任务吗?
43 Thread1 :main函数在等我完成任务吗?
44 线程1已经结束
45 线程2已经结束
46 线程3已经结束

编译方法:gcc threadSample.c -o threadSample -lpthread

代码非常简单,请自行理解吧

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!