我是靠谱客的博主 感动大炮,最近开发中收集的这篇文章主要介绍Linux下使用原子锁实现线程同步,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

以下内容仅做为学习笔记使用,主要分析学习过程中遇到的问题。如有什么理解不到位的地方,还请多多指教!

原子锁是linux内核同步的一种机制,下面将其应用到线程同步中来。贴上代码

#include <alsa/iatomic.h>
#include <pthread.h>
#include <stdio.h>
// 定义一个原子变量
static atomic_t g_atomic = ATOMIC_INIT(1);
// 定义共享资源
static volatile int g_i = 0;
/* 定义线程处理函数 */
//#define atomic_dec_and_test(g_atomic) 1
void *thr1_handle(void *arg)
{
while (1) {
if (atomic_dec_and_test(&g_atomic)) {
printf("in thread %lu g_i = %dn", pthread_self(), ++g_i);
}
atomic_inc(&g_atomic);
sleep(1);
}
return NULL;
}
void *thr2_handle(void *arg)
{
while (1) {
if (atomic_dec_and_test(&g_atomic)) {
printf("in thread %lu g_i = %dn", pthread_self(), --g_i);
}
atomic_inc(&g_atomic);
sleep(1);
}
return NULL;
}
/* 主函数 */
int main()
{
pthread_t tid1, tid2;
if (pthread_create(&tid1, NULL, thr1_handle, NULL) != 0) {
fprintf(stderr, "create thread1 failed!n");
return 1;
}
if (pthread_create(&tid2, NULL, thr2_handle, NULL) != 0) {
fprintf(stderr, "create thread2 failed!n");
return 2;
}
pthread_join(tid1, NULL);
pthread_join(tid2, NULL);
return 0;
}

不错,这里用 atomic 做了同步,程序如预期输出:

in thread 3076184896 g_i = 1
in thread 3067792192 g_i = 0
in thread 3076184896 g_i = 1
in thread 3067792192 g_i = 0
in thread 3076184896 g_i = 1
in thread 3067792192 g_i = 0
in thread 3076184896 g_i = 1
in thread 3067792192 g_i = 0
in thread 3076184896 g_i = 1
in thread 3067792192 g_i = 0
in thread 3076184896 g_i = 1
in thread 3067792192 g_i = 0
in thread 3076184896 g_i = 1
in thread 3067792192 g_i = 0
in thread 3076184896 g_i = 1
没有任何问题。

但是这里最应该记住的是,如果不同不产生的后果。

如果将上面的

//#define atomic_dec_and_test(g_atomic) 1
注释打开,即不让 atomic 来提供同步工作,那么程序的结果是混乱的。 如下:

in thread 3075713856 g_i = 1
in thread 3067321152 g_i = 0
in thread 3067321152 g_i = -1
in thread 3075713856 g_i = 1
in thread 3067321152 g_i = 0
in thread 3075713856 g_i = 1
in thread 3075713856 g_i = 2
in thread 3067321152 g_i = 0

在上面这种混乱的输出,源于 ++/-- 并非原子操作,在执行 ++/-- 的操作过程中,很可能发生线程切换,这是上一线程所做的修改还没来得及保存到内存,这样就出现数据的不同步。


本文参考了网上资源:

http://www.netfoucs.com/fenglifeng1987/article/details/8172975 这里讲述了 atomic 的相关API

http://www.linuxidc.com/Linux/2011-06/37402.htm  这里讲述了在 2.6.18 内核后删除atomic,并提供了一个替代品


文章最后感谢论坛网友的分析:

http://bbs.chinaunix.net/thread-4134133-1-1.html

最后

以上就是感动大炮为你收集整理的Linux下使用原子锁实现线程同步的全部内容,希望文章能够帮你解决Linux下使用原子锁实现线程同步所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(54)

评论列表共有 0 条评论

立即
投稿
返回
顶部