先看执行的结果:
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 = 010 Thread3 : I‘m thread 3nd11 thread3 : number = 212 thread3 : number = 313 thread2 : number = 414 thread1 : number = 515 thread3 : number = 616 thread2 : number = 717 thread3 : number = 818 thread1 : number = 919 thread3 : number = 1020 thread2 : number = 1121 thread3 : number = 1222 thread2 : number = 1323 thread1 : number = 1424 thread3 : number = 1525 thread3 : number = 1626 thread2 : number = 1727 thread1 : number = 1828 thread3 : number = 1929 thread2 : number = 2030 thread3 : number = 2131 thread1 : number = 2232 thread3 : number = 2333 thread2 : number = 2434 thread3 : number = 2535 thread2 : number = 2636 thread1 : number = 2737 thread3 : number = 2838 thread3 : number = 2939 thread2 : number = 3040 thread1 : number = 3141Thread3 :main函数在等我完成任务吗?
42Thread2 :main函数在等我完成任务吗?
43Thread1 :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个线程 10pthread_mutex_t mut;
11int number=0;
12int i;
13 14void *thread1(){
15 printf ("Thread1 : I‘m thread 1thn");
16for (i = 0; i < MAX; i++)
17 {
18 printf("thread1 : number = %dn",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 29void *thread2(){
30 printf("Thread2 : I‘m thread 2ndn");
31 32for (i = 0; i < MAX; i++)
33 {
34 printf("thread2 : number = %dn",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 45void *thread3(){
46 printf("Thread3 : I‘m thread 3ndn");
47 48for (i = 0; i < MAX; i++)
49 {
50 printf("thread3 : number = %dn",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 61void thread_create(void){ int temp;
62 memset(&thread, 0, sizeof(thread));
63/*创建线程*/ 64if((temp = pthread_create(&thread[0], NULL, thread1, NULL)) != 0)
65 printf("线程1创建失败!n");
66else 67 printf("线程1被创建n");
68 69if((temp = pthread_create(&thread[1], NULL, thread2, NULL)) != 0)
70 printf("线程2创建失败");
71else 72 printf("线程2被创建n");
73 74if((temp = pthread_create(&thread[2], NULL, thread3, NULL)) != 0)
75 printf("线程3创建失败");
76else 77 printf("线程3被创建n");
78}
79 80void thread_wait(void){
81/*等待线程结束*/ 82if(thread[0] !=0){
83 pthread_join(thread[0],NULL);
84 printf("线程1已经结束n");
85 }
86if(thread[1] !=0){
87 pthread_join(thread[1],NULL);
88 printf("线程2已经结束n");
89 }
90if(thread[2] !=0){
91 pthread_join(thread[2],NULL);
92 printf("线程3已经结束n");
93 }
94}
95 96int main()
97{
98/*用默认属性初始化互斥锁*/ 99 pthread_mutex_init(&mut,NULL);
100101 printf("主函数正在创建线程,...n");
102 thread_create();
103 printf("主函数正在等待线程结束...n");
104 thread_wait();
105106return0;
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 3nd11 thread3 : number = 212 thread3 : number = 313 thread2 : number = 414 thread1 : number = 515 thread3 : number = 616 thread2 : number = 717 thread3 : number = 818 thread1 : number = 919 thread3 : number = 1020 thread2 : number = 1121 thread3 : number = 1222 thread1 : number = 1323 thread2 : number = 1424 thread3 : number = 1525 thread3 : number = 1626 thread2 : number = 1727 thread1 : number = 1828 thread3 : number = 1929 thread2 : number = 2030 thread3 : number = 2131 thread1 : number = 2232 thread3 : number = 2333 thread2 : number = 2434 thread3 : number = 2535 thread1 : number = 2636 thread2 : number = 2737 thread3 : number = 2838 thread3 : number = 2939 thread2 : number = 3040 thread1 : number = 3141Thread3 :main函数在等我完成任务吗?
42Thread2 :main函数在等我完成任务吗?
43Thread1 :main函数在等我完成任务吗?
44线程1已经结束
45线程2已经结束
46 线程3已经结束
编译方法:gcc threadSample.c -o threadSample -lpthread
代码非常简单,请自行理解吧
原文:https://www.cnblogs.com/guochaoxxl/p/12584529.html
【说明】:本文章由站长整理发布,文章内容不代表本站观点,如文中有侵权行为,请与本站客服联系(QQ:254677821)!