学习参考博文:
Linux多线程编程初探
Linux系统编程学习相关博文
常规学习Linux系统编程的内容是复杂且繁多的,不推荐刚开始接触代码的朋友去学习,所以介绍Linux系统编程的目的主要是以应用开发为主。
在Linux系统中,操作系统提供了一系列的API,详细看下图
1. 线程
创建 pthread_creat()
退出 pthread_exit()
等待 pthread_join()
获取ID pthread_self()
2. 互斥锁
创建 pthread_mutex_init()
销毁 pthread_mutex_destroy()
加锁 pthread_mutex_lock()
解锁 pthread_mutex_unlock()
3. 条件
创建 pthread_cond_init()
销毁 pthread_cond_destroy()
触发 pthread_cond_signal()
广播 pthread_cond_broadcast()
等待 pthread_cond_wait() / pthread_cond_timewait()
#include
int pthread_create(pthread_t *restrict tidp, const pthread_attr_t *restrict attr, void *(*start_rtn)(void *), void *restrict arg);
1. 函数功能:创建线程
2. 形参说明:
tidp:线程ID
attr:线程属性。暂可以把它设置为NULL,以创建默认属性的线程。
start_rtn:新创建线程的运行函数
arg:函数运行传递的参数
3. 返回值:成功返回0,失败返回错误编号
#include
int pthread_exit(void *rval_ptr);
1. 函数功能:线程退出
2. 形参说明:
rival_ptr:退出时返回的数据
#include
int pthread_join(pthread_t thread, void **rval_ptr);
1. 函数功能:调用这个函数的线程将一直阻塞,直到指定的线程调用pthread_exit、从启动例程中返回或者被取消。
2. 形参说明:
thread:线程ID
rival_ptr:线程退出时返回的数据
3. 返回值:成功返回0,失败返回错误编号
#include
pthread_t pthread_self(void);
1. 函数功能:获取线程ID
2. 返回值:调用线程的ID
#include
int pthread_mutex_init(pthread_mutex_t *restrict mutex, const pthread_mutexattr_t *restrict attr);
1. 函数功能:创建互斥锁
2. 形参说明:
mutex:锁ID
attr:锁的属性。默认的属性初始化互斥量,只需把attr设置为NULL。
3. 返回值:成功返回0,失败返回错误编号
#include
int pthread_mutex_destroy(pthread_mutex_t *mutex);
1. 函数功能:销毁互斥锁
2. 形参说明:
mutex:锁ID
3. 返回值:成功返回0,失败返回错误编号
#include
int pthread_mutex_lock(pthread_mutex_t *mutex);
1. 函数功能:加锁
2. 形参说明:
mutex:锁ID
3. 返回值:成功返回0,失败返回错误编号
#include
int pthread_mutex_unlock(pthread_mutex_t *mutex);
1. 函数功能:解锁
2. 形参说明:
mutex:锁ID
3. 返回值:成功返回0,失败返回错误编号
#include
int pthread_cond_init(pthread_cond_t *restrict cond, const pthread_condattr_t *restrict attr);
1. 函数功能:创建条件变量
2. 形参说明:
cond:条件ID
attr:条件属性。除非需要创建一个非默认属性的条件变量,否则该参数可以设置为NULL。
3. 返回值:成功返回0,失败返回错误编号
#include
int pthread_cond_destroy(pthread_cond_t *cond);
1. 函数功能:销毁条件变量
2. 形参说明:
cond:条件ID
3. 返回值:成功返回0,失败返回错误编号
#include
int pthread_cond_wait(pthread_cond_t *restrict cond, pthread_mutex_t *restrict mutex);
1. 函数功能:等待条件为真
2. 形参说明:
cond:条件ID
mutex:锁ID
3. 返回值:成功返回0,失败返回错误编号
#include
int pthread_cond_signal(pthread_cond_t *cond);
1. 函数功能:触发条件
2. 形参说明:
cond:条件ID
3. 返回值:成功返回0,失败返回错误编号
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4
5 #include <pthread.h>
6
7 // 1. int pthread_create(pthread_t *restrict tidp, const pthread_attr_t *restrict attr, void *(* start_rtn)(void *), void *restrict arg);
8 // 2. pthread_t pthread_self(void);
9 // 3. int pthread_join(pthread_t thread, void **rval_ptr);
10 // 4. int pthread_exit(void *rval_ptr);
11
12 void *func1(void *arg)
13 {
14 static int data = 10;
15
16 printf("t1: %ld\n", pthread_self()); //打印线程ID
17 printf("t1: arg = %d\n", *((int *)arg)); //打印传递的参数
18
19 pthread_exit((void *)(&data)); //线程退出并返回数据
20 }
21
22 int main()
23 {
24 int ret = 0;
25 int param = 100;
26 int *pret = NULL;
27
28 pthread_t t1; //线程ID
29
30 ret = pthread_create(&t1, NULL, func1, (void *)¶m); //创建线程
31
32 if(ret == 0){
33 printf("main: %ld, pthread create success\n", pthread_self());
34 }
35
36 pthread_join(t1, (void **)(&pret)); //等待线程退出
37
38 printf("main: pret = %d\n", *pret); //打印线程退出时反馈的的数据
39
40 return 0;
41 }
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4
5 #include <pthread.h>
6
7 // 1. int pthread_mutex_init(pthread_mutex_t *restrict mutex, const pthread_mutexattr_t *restric t attr);
8 // 2. int pthread_mutex_destroy(pthread_mutex_t *mutex);
9 // 3. int pthread_mutex_lock(pthread_mutex_t *mutex);
10 // 4. int pthread_mutex_unlock(pthread_mutex_t *mutex);
11
12 int g_data = 0;
13
14 pthread_mutex_t mutex; //锁
15
16 void *func1(void *arg)
17 {
18 printf("t1: %ld\n", pthread_self()); //打印线程ID
19 printf("t1: arg = %d\n", *((int *)arg)); //打印传递过来的数据
20
21 pthread_mutex_lock(&mutex); //加锁
22
23 while(1){
24 printf("t1: %d\n", g_data++);
25
26 if(g_data == 3){
27 printf("t1 quit===========================\n");
28 pthread_mutex_unlock(&mutex); //当g_data = 3时解锁
29 pthread_exit(NULL); //线程退出
30 }
31
32 sleep(1);
33 }
34
35 }
36
37 void *func2(void *arg)
38 {
39 printf("t2: %ld\n", pthread_self());
40 printf("t2: arg = %d\n", *((int *)arg));
41
42 while(1){
43 pthread_mutex_lock(&mutex); //加锁
44
45 printf("t2: %d\n", g_data++);
46
47 pthread_mutex_unlock(&mutex); //解锁
48
49 sleep(1);
50 }
51 }
52
53 int main()
54 {
55 int ret = 0;
56 int param = 100;
57
58 pthread_t t1;
59 pthread_t t2;
60
61 pthread_mutex_init(&mutex, NULL); //创建互斥锁
62
63 ret = pthread_create(&t1, NULL, func1, (void *)¶m); //创建线程1
64
65 if(ret == 0){
66 //printf("main: t1:%ld, pthread create success\n", pthread_self());
67 }
68
69 ret = pthread_create(&t2, NULL, func2, (void *)¶m); //创建线程2
70
71 if(ret == 0){
72 //printf("main: t2:%ld, pthread create success\n", pthread_self());
73 }
74
75 pthread_join(t1, NULL); //等待线程1退出
76 pthread_join(t2, NULL); //等待线程2退出
77
78 pthread_mutex_destroy(&mutex); //销毁互斥锁
79
80 return 0;
81 }
1. 动态初始化
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4
5 #include <pthread.h>
6
7 // 1. int pthread_create(pthread_t *restrict tidp, const pthread_attr_t *restrict attr, void *(* start_rtn)(void *), void *restrict arg);
8 // 2. pthread_t pthread_self(void);
9 // 3. int pthread_join(pthread_t thread, void **rval_ptr);
10 // 4. int pthread_exit(void *rval_ptr);
11 // 5. int pthread_mutex_init(pthread_mutex_t *restrict mutex, const pthread_mut exattr_t *res trict attr);
12 // 6. int pthread_mutex_destroy(pthread_mutex_t *mutex);
13 // 7. int pthread_mutex_lock(pthread_mutex_t *mutex);
14 // 8. int pthread_mutex_unlock(pthread_mutex_t *mutex);
15 // 9. int pthread_cond_init(pthread_cond_t *restrict cond, const pthread_condattr_t *restrict at tr);
16 // 10. int pthread_cond_destroy(pthread_cond_t *cond);
17 // 11. int pthread_cond_wait(pthread_cond_t *restrict cond, pthread_mutex_t *restrict mutex);
18 // 12. int pthread_cond_signal(pthread_cond_t *cond);
19
20 int g_data = 0;
21
22 pthread_cond_t cond; //条件变量
23 pthread_mutex_t mutex; //锁
24
25 void *func1(void *arg)
26 {
27 printf("t1: %ld\n", pthread_self()); //打印线程ID
28 printf("t1: arg = %d\n", *((int *)arg)); //打印传递过来的数据
29
30 while(1){
31 pthread_cond_wait(&cond, &mutex); //等待条件为真
32
33 printf("t1 run=========================\n");
34 printf("t1: %d\n", g_data);
35 g_data = 0;
36
37 sleep(1);
38 }
39 }
40
41 void *func2(void *arg)
42 {
43 printf("t2: %ld\n", pthread_self());
44 printf("t2: arg = %d\n", *((int *)arg));
45
46 while(1){
47 pthread_mutex_lock(&mutex); //加锁
48
49 printf("t2: %d\n", g_data++);
50 if(g_data == 3){
51 pthread_cond_signal(&cond); //当g_data = 3时,触发条件,唤醒等待该条件的线程
52 }
53
54 pthread_mutex_unlock(&mutex); //解锁
55
56 sleep(1);
57 }
58 }
59
60 int main()
61 {
62 int ret = 0;
63 int param = 100;
64
65 pthread_t t1;
66 pthread_t t2;
67
68 pthread_cond_init(&cond, NULL); //创建条件变量
69 pthread_mutex_init(&mutex, NULL); //创建互斥锁
70
71 ret = pthread_create(&t1, NULL, func1, (void *)¶m); //创建线程1
72
73 if(ret == 0){
74 //printf("main: t1:%ld, pthread create success\n", pthread_self());
75 }
76
77 ret = pthread_create(&t2, NULL, func2, (void *)¶m); //创建线程2
78
79 if(ret == 0){
80 //printf("main: t2:%ld, pthread create success\n", pthread_self());
81 }
82
83 pthread_join(t1, NULL); //等待线程1退出
84 pthread_join(t2, NULL); //等待线程2退出
85
86 pthread_cond_destroy(&cond); //销毁条件变量
87 pthread_mutex_destroy(&mutex); //销毁互斥锁
88
89 return 0;
90 }
2. 静态初始化
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4
5 #include <pthread.h>
6
7 // 1. int pthread_create(pthread_t *restrict tidp, const pthread_attr_t *restrict attr, void *(* start_rtn)(void *), void *restrict arg);
8 // 2. pthread_t pthread_self(void);
9 // 3. int pthread_join(pthread_t thread, void **rval_ptr);
10 // 4. int pthread_exit(void *rval_ptr);
11 // 5. int pthread_mutex_init(pthread_mutex_t *restrict mutex, const pthread_mut exattr_t *res trict attr);
12 // 6. int pthread_mutex_destroy(pthread_mutex_t *mutex);
13 // 7. int pthread_mutex_lock(pthread_mutex_t *mutex);
14 // 8. int pthread_mutex_unlock(pthread_mutex_t *mutex);
15 // 9. int pthread_cond_init(pthread_cond_t *restrict cond, const pthread_condattr_t *restrict at tr);
16 // 10. int pthread_cond_destroy(pthread_cond_t *cond);
17 // 11. int pthread_cond_wait(pthread_cond_t *restrict cond, pthread_mutex_t *restrict mutex);
18 // 12. int pthread_cond_signal(pthread_cond_t *cond);
19
20 int g_data = 0;
21
22 pthread_cond_t cond = PTHREAD_COND_INITIALIZER; //初始化条件变量
23 pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; //初始化互斥锁
24
25 void *func1(void *arg)
26 {
27 printf("t1: %ld\n", pthread_self()); //打印线程ID
28 printf("t1: arg = %d\n", *((int *)arg)); //打印传递过来的值
29
30 while(1){
31 pthread_cond_wait(&cond, &mutex); //等待条件为真
32
33 printf("t1 run=========================\n");
34 printf("t1: %d\n", g_data);
35 g_data = 0;
36
37 sleep(1);
38 }
39 }
40
41 void *func2(void *arg)
42 {
43 printf("t2: %ld\n", pthread_self()); //打印线程ID
44 printf("t2: arg = %d\n", *((int *)arg)); //打印传递过来的值
45
46 while(1){
47 pthread_mutex_lock(&mutex); //加锁
48
49 printf("t2: %d\n", g_data++);
50 if(g_data == 3){
51 pthread_cond_signal(&cond); //当g_data = 3时,触发条件,唤醒等待该条件的线程
52 }
53
54 pthread_mutex_unlock(&mutex); //解锁
55
56 sleep(1);
57 }
58 }
59
60 int main()
61 {
62 int ret = 0;
63 int param = 100;
64
65 pthread_t t1;
66 pthread_t t2;
67
68 //pthread_cond_init(&cond, NULL);
69 //pthread_mutex_init(&mutex, NULL);
70
71 ret = pthread_create(&t1, NULL, func1, (void *)¶m); //创建线程1
72
73 if(ret == 0){
74 //printf("main: t1:%ld, pthread create success\n", pthread_self());
75 }
76
77 ret = pthread_create(&t2, NULL, func2, (void *)¶m); //创建线程2
78
79 if(ret == 0){
80 //printf("main: t2:%ld, pthread create success\n", pthread_self());
81 }
82
83 pthread_join(t1, NULL); //等待线程1退出
84 pthread_join(t2, NULL); //等待线程2退出
85
86 pthread_cond_destroy(&cond); //销毁条件变量
87 pthread_mutex_destroy(&mutex); //销毁互斥锁
88
89 return 0;
90 }