我是靠谱客的博主 虚拟音响,这篇文章主要介绍Linux线程同步-互斥锁这是学习C语言中文网的笔记简介函数声明不加锁的代码实例(Linux线程同步详解)编译运行输出结果加上互斥锁后编译运行输出结果,现在分享给大家,希望可以做个参考。
这是学习C语言中文网的笔记
简介
互斥锁(Mutex)又称互斥量或者互斥体,是最简单也最有效地一种线程同步机制。互斥锁的用法和实际生活中的锁非常类似,当一个线程访问公共资源时,会及时地“锁上”该资源,阻止其它线程访问;访问结束后再进行“解锁”操作,将该资源让给其它线程访问。
函数声明
复制代码
1
2
3
4
5
6
7
8pthread_mutex_t lock; /* 实例化互斥锁结构体 */ pthread_mutex_init(&lock, NULL); /* 动态初始化, 成功返回0,失败返回非0 */ pthread_mutex_t thread_mutex = PTHREAD_MUTEX_INITIALIZER; /* 静态初始化 */ pthread_mutex_lock(&lock); /* 阻塞的锁定互斥锁 */ pthread_mutex_trylock(&thread_mutex);/* 非阻塞的锁定互斥锁,成功获得互斥锁返回0,如果未能获得互斥锁,立即返回一个错误码 */ pthread_mutex_unlock(&lock); /* 解锁互斥锁 */ pthread_mutex_destroy(&lock) /* 销毁互斥锁 */
不加锁的代码实例(Linux线程同步详解)
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42#include <stdio.h> #include <stdlib.h> #include <pthread.h> int ticket_sum = 10; void *sell_ticket(void *arg){ int i; for ( i = 0; i < 10; i++) { if(ticket_sum >0) { sleep(1); printf("%u 卖第 %d 张票n", (unsigned int)pthread_self(), 10 - ticket_sum +1 ); ticket_sum --; } } return 0; } int main(){ int flag, i ; void *ans; pthread_t tids[4]; for ( i = 0; i < 4; i++) { flag = pthread_create(&tids[i], NULL, &sell_ticket, NULL); if (flag != 0) { printf("creat thread fail!!"); return 0; } } sleep(100); for ( i = 0; i < 4; i++) { flag = pthread_join(tids[i], &ans); if (flag != 0){ printf("tid=%d join fail !!" , (int)tids[i]); return 0; } } return 0; }
编译运行
复制代码
1
2
3root@192:/usr/local/workspace# gcc -o thread thread.c -lpthread root@192:/usr/local/workspace# ./thread
输出结果
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20root@192:/usr/local/workspace# gcc -o thread thread.c -lpthread thread.c: In function ‘sell_ticket’: thread.c:14:13: warning: implicit declaration of function ‘sleep’ [-Wimplicit-function-declaration] sleep(1); ^ root@192:/usr/local/workspace# ./thread 2840495872 卖第 1 张票 2848888576 卖第 2 张票 2865673984 卖第 3 张票 2857281280 卖第 4 张票 2840495872 卖第 5 张票 2857281280 卖第 5 张票 2848888576 卖第 5 张票 2865673984 卖第 5 张票 2840495872 卖第 9 张票 2857281280 卖第 9 张票 2848888576 卖第 9 张票 2865673984 卖第 9 张票 2840495872 卖第 13 张票
加上互斥锁后
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47#include <stdio.h> #include <stdlib.h> #include <pthread.h> pthread_mutex_t lock; int ticket_sum = 10; void *sell_ticket(void *arg){ int i; for ( i = 0; i < 10; i++) { pthread_mutex_lock(&lock); if(ticket_sum >0) { sleep(1); printf("%u 卖第 %d 张票n", (unsigned int)pthread_self(), 10 - ticket_sum +1 ); ticket_sum --; } pthread_mutex_unlock(&lock); } return 0; } int main(){ int flag, i ; void *ans; pthread_t tids[4]; pthread_mutex_init(&lock,NULL); for ( i = 0; i < 4; i++) { flag = pthread_create(&tids[i], NULL, &sell_ticket, NULL); if (flag != 0) { printf("creat thread fail!!"); return 0; } } sleep(100); for ( i = 0; i < 4; i++) { flag = pthread_join(tids[i], &ans); if (flag != 0){ printf("tid=%d join fail !!" , (int)tids[i]); return 0; } } pthread_mutex_destroy(&lock); return 0; }
编译运行
复制代码
1
2
3root@192:/usr/local/workspace# gcc -o thread thread.c -lpthread root@192:/usr/local/workspace# ./thread
输出结果
复制代码
1
2
3
4
5
6
7
8
9
10
113386459904 卖第 1 张票 3386459904 卖第 2 张票 3386459904 卖第 3 张票 3386459904 卖第 4 张票 3386459904 卖第 5 张票 3386459904 卖第 6 张票 3386459904 卖第 7 张票 3386459904 卖第 8 张票 3386459904 卖第 9 张票 3386459904 卖第 10 张票
最后
以上就是虚拟音响最近收集整理的关于Linux线程同步-互斥锁这是学习C语言中文网的笔记简介函数声明不加锁的代码实例(Linux线程同步详解)编译运行输出结果加上互斥锁后编译运行输出结果的全部内容,更多相关Linux线程同步-互斥锁这是学习C语言中文网内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复