Linux编程基础之多线程

  1 #include <stdio.h>
2 #include <pthread.h>
3 #include <stdlib.h>
4 #include <unistd.h>
5 #include <sys/types.h>
6 #include <sys/stat.h>
7
8 /***********************************************************
9 功能说明:多线程示例
10 1)多线程的创建
11 2)多线程等待(阻塞)
12 3)线程执行指定的函数(及函数的参数传递)
13 4)同进程下多个线程之间的数据共享
14
15 author: [email protected]
16
17 ***********************************************************/
18 void *thread1(void);
19 void *thread2(void *arg);
20
21 void *thread3(char *arg);
22
23
24 int test = 99; //全局变量
25
26
27 int main(int argc, char * argv[])
28 {
29
30 pthread_t tid1,tid2,tid3; //线程ID
31
32 {
33 int rc;
34 int shere=0;
35
36 //创建线程1
37 rc = pthread_create(&tid1, NULL, (void*)thread1, NULL);//创建第一个线程,该线程执行thread1函数,参数为空
38 printf("create 1st thread return %d\n",rc);
39 if (rc) {
40 fprintf(stderr, "ERROR: calling pthread_create() at %s:%d\n",
41 __FILE__, __LINE__);
42 exit(1);
43 }
44
45 int ar = 10;
46 int *arg = &ar;
47
48 //创建线程2
49 rc = pthread_create(&tid2, NULL, (void*)thread2, (void*)arg);//创建第二个线程,该线程执行thread2函数,参数为(void *)arg,其实质是个int *类型
50 printf("create 2st thread return %d\n",rc);
51 if (rc) {
52 fprintf(stderr, "ERROR: calling pthread_create() at %s:%d\n",
53 __FILE__, __LINE__);
54 exit(1);
55 }
56 //创建线程3
57 char *str = "china";
58
59 rc = pthread_create(&tid3, NULL, (void*)thread3, str);//创建第三个线程,该线程执行thread3函数,参数为(char *)str
60 printf("create 3st thread return %d\n",rc);
61 if (rc) {
62 fprintf(stderr, "ERROR: calling pthread_create() at %s:%d\n",
63 __FILE__, __LINE__);
64 exit(1);
65 }
66
67 //pthread_join 进程等待
68 if (pthread_join(tid1, NULL) != 0) {
69 fprintf(stderr, "ERROR: calling pthread_join()\n");//线程等待,主进程阻塞,直到执行完该线程
70 exit(1);
71 }
72 if (pthread_join(tid2, NULL) != 0) {
73 fprintf(stderr, "ERROR: calling pthread_join()\n");
74 exit(1);
75 }
76 if (pthread_join(tid3, NULL) != 0) {
77 fprintf(stderr, "ERROR: calling pthread_join()\n");
78 exit(1);
79 }
80 printf("test ==%d\n",test);//打印全局变量的最终结果,该变量在第一个和第三个线程中被操作过。
81
82 }
83 }
84 /**********************************************************
85
86 方法功能:线程1执行的函数定义
87
88 **********************************************************/
89 void *thread1(void)
90 {
91 test++;//直接操作全局变量
92
93 printf("hello ,this is thread1\n");
94 sleep(3);
95
96 }
97 /**********************************************************
98
99 方法功能:线程2执行的函数原型,参数void *arg
100
101 **********************************************************/
102 void *thread2(void *arg)
103 {
104 printf("this is thread2\n");
105 int *a = (int*)arg;//将参数还原成int *类型
106
107 printf("arg of thread2 is %d\n", *a);
108
109
110
111
112 sleep(3);
113
114 }
115
116 /**********************************************************
117
118 方法功能:线程3,传递String数据
119
120 **********************************************************/
121
122 void *thread3(char *arg)
123 {
124
125 printf("this is thread3\n");
126 char *s = (char *)arg;//将参数还原为char *类型
127 printf("arg of thread3 is %s\n", s);
128
129 printf("thread3:test-->%d\n", test);
130 test++; //直接操作全局变量
131
132 }


多线程在Linux编程中也是很重要的一部分知识,因为这线程关乎程序的性能,性能就是Money呀,

代码示例1:mythread.c

 


运行结果如下:

Linux编程基础之多线程

注:涉及知识点

①:创建线程API: pthread_create

②:线程等待 pthread_join

③:线程执行的函数参数的使用

你可能感兴趣的:(linux)