转:http://blog.csdn.net/aniao/article/details/5802015

APUE上,关于条件锁。其中有这么几条总结:

1.使用条件锁前必须先锁住对应的互斥锁。

2.条件锁进入阻塞(pthread_cond_wait)时自动解开对应互斥锁,而一旦跳出阻塞立即再次取得互斥锁,而这两个操作都是原子操作。

好,现在考虑到这一点,假如有如下函数:

void* run(void *s)
{
pthread_mutex_lock(&mutex);
while(i == )
{
printf("线程%u进入等待状态\n", (unsigned int)pthread_self());
pthread_cond_wait(&cond_l, &mutex);
}
printf("已经解开%u\n", (unsigned int)pthread_self());
pthread_mutex_unlock(&mutex);
i = ; return ((void *) );
}

根据前面两条规则,我们可以知道,如果多个线程同时调用这个函数,当一个线程取得同步锁之后,其他线程就会阻塞在pthread_mutex_lock函数,而当那个取得锁的线程执行到pthread_cond_wait并阻塞之后,在从这个函数返回(条件满足)之前,会释放掉锁,所以其他线程也能一个一个都执行到pthread_cond_wait这里阻塞。这时就有多个线程阻塞在这里了。

假设这时候在另外某个线程条件被满足,并发出了pthread_cond_signal,那么这么多阻塞的线程会不会全部一下就都被解开了呢?

答案是否。

因为根据第二条规则,从阻塞的函数返回并尝试再次锁住互斥锁,这是一个原子操作。也就是说,第一个成功解套的线程会再次锁上互斥锁,而其他线程这时候要想跳出阻塞状态就不可能了,因为他们无法取得互斥锁,只能继续等待(根据我的测试是等待下一次pthread_cond_singal。

(以上是错误的,后来发现,原来pthread_cond_signal本来就只会唤醒一个条件锁,而实验证明,唤醒的顺序跟阻塞在条件锁的顺序相同)

#include <stdio.h>
#include <error.h>
#include <pthread.h>
#include <unistd.h> pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t cond_l = PTHREAD_COND_INITIALIZER; int i = ;
void* run(void *); void main(int argc, char **argv)
{
pthread_t pid1;
pthread_t pid2;
pthread_t pid3;
pthread_t pid4; pthread_create(&pid1, NULL, run, NULL );
printf("new thread:%u\n", (unsigned int)pid1);
sleep(); pthread_create(&pid2, NULL, run, NULL );
printf("new thread:%u\n", (unsigned int)pid2);
sleep(); pthread_create(&pid3, NULL, run, NULL );
printf("new thread:%u\n", (unsigned int)pid3);
sleep(); pthread_create(&pid4, NULL, run, NULL );
printf("new thread:%u\n", (unsigned int)pid4);
sleep(); //修改
//pthread_mutex_lock(&mutex); i = ;
pthread_cond_signal(&cond_l);
printf("release signal\n");
sleep(); i = ;
pthread_cond_signal(&cond_l);
printf("release signal\n");
sleep(); pthread_join(pid1, NULL );
pthread_join(pid2, NULL );
pthread_join(pid3, NULL );
pthread_join(pid4, NULL );
} void* run(void *s)
{
pthread_mutex_lock(&mutex);
while(i == )
{
printf("线程%u进入等待状态\n", (unsigned int)pthread_self());
pthread_cond_wait(&cond_l, &mutex);
}
printf("已经解开%u\n", (unsigned int)pthread_self());
pthread_mutex_unlock(&mutex);
i = ; return ((void *) );
}

最后的输出是:

new thread:3085007776

线程3085007776进入等待状态

new thread:3076615072

线程3076615072进入等待状态

new thread:3068222368

线程3068222368进入等待状态

new thread:3059829664

线程3059829664进入等待状态

release signal

已经解开3085007776

release signal

已经解开3076615072

一切正常,每次pthread_cond_signal就能放掉一个线程。那么为了验证前面我的分析是正确的,加入在执行pthread_cond_signal的时候,阻塞在对应条件锁的pthread_cond_wait处的线程的互斥锁全都是被锁住的,还会有线程能成功解套么?看以下代码:

#include <stdio.h>
#include <error.h>
#include <pthread.h>
#include <unistd.h> pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t cond_l = PTHREAD_COND_INITIALIZER; int i = ;
void* run(void *); void main(int argc, char **argv)
{
pthread_t pid1;
pthread_t pid2;
pthread_t pid3;
pthread_t pid4; pthread_create(&pid1, NULL, run, NULL );
printf("new thread:%u\n", (unsigned int)pid1);
sleep(); pthread_create(&pid2, NULL, run, NULL );
printf("new thread:%u\n", (unsigned int)pid2);
sleep(); pthread_create(&pid3, NULL, run, NULL );
printf("new thread:%u\n", (unsigned int)pid3);
sleep(); pthread_create(&pid4, NULL, run, NULL );
printf("new thread:%u\n", (unsigned int)pid4);
sleep(); //修改
pthread_mutex_lock(&mutex); i = ;
pthread_cond_signal(&cond_l);
printf("release signal\n");
sleep(); i = ;
pthread_cond_signal(&cond_l);
printf("release signal\n");
sleep(); pthread_join(pid1, NULL );
pthread_join(pid2, NULL );
pthread_join(pid3, NULL );
pthread_join(pid4, NULL );
} void* run(void *s)
{
pthread_mutex_lock(&mutex);
while(i == )
{
printf("线程%u进入等待状态\n", (unsigned int)pthread_self());
pthread_cond_wait(&cond_l, &mutex);
}
printf("已经解开%u\n", (unsigned int)pthread_self());
pthread_mutex_unlock(&mutex);
i = ; return ((void *) );
}

注意带注释的地方,在执行pthread_cond_signal之前,我又把互斥锁锁住了。之所以这里敢这么写,是因为其他几个子线程最后卡在pthread_cond_wait的时候都会把锁给释放掉的,所以我能在主线程里取得互斥锁。这样的话,其他子线程接到条件满足的信号后还会从等待中跳出来吗?运行结果如下:

new thread:3085290400

线程3085290400进入等待状态

new thread:3076897696

线程3076897696进入等待状态

new thread:3068504992

线程3068504992进入等待状态

new thread:3060112288

线程3060112288进入等待状态

release signal

release signal

Oh,No,果然,没有一个线程跑出来。事实上,如果不是这么改,而是让每个线程在run函数最后不释放互斥锁,最后只会有第一个跑出来的线程解套成功。所以,从目前来看,我的分析应该是正确的。

关于一点pthread_cond_t条件锁的思考以及实验的更多相关文章

  1. Linux 开发之线程条件锁那些事

    2019独角兽企业重金招聘Python工程师标准>>> 条件锁即在一定条件下触发,那什么时候适合用条件锁呢,那当然是你在等待一个符合的条件下触发.一个常用的例子就是在线程中无限循环执 ...

  2. 47. Permutations II(medium, backtrack, 重要, 条件较难思考)

    Given a collection of numbers that might contain duplicates, return all possible unique permutations ...

  3. python线程condition条件锁应用实例

    import time import threading # 吃火锅鱼丸 guo = [] suo = threading.Condition() #条件锁 # 生产者负责生产 class Produ ...

  4. Python的条件锁与事件共享

    1:事件机制共享队列: 利用消息机制在两个队列中,通过传递消息,实现可以控制的生产者消费者问题要求:readthread读时,writethread不能写:writethread写时,readthre ...

  5. 死磕 java同步系列之ReentrantLock源码解析(二)——条件锁

    问题 (1)条件锁是什么? (2)条件锁适用于什么场景? (3)条件锁的await()是在其它线程signal()的时候唤醒的吗? 简介 条件锁,是指在获取锁之后发现当前业务场景自己无法处理,而需要等 ...

  6. 深入理解java:2.3.2. 并发编程concurrent包 之重入锁/读写锁/条件锁

    重入锁 Java中的重入锁(即ReentrantLock)   与JVM内置锁(即synchronized)一样,是一种排它锁. ReentrantLock提供了多样化的同步,比如有时间限制的同步(定 ...

  7. 条件锁Condition

    """设计场景:timo先说一句,亚索再说一句timo: timo队长正在待命yasuo: 面对疾风吧timo: timo整装待发yasuo: 哈杀gay "& ...

  8. 关于web多标签多条件筛选的思考以及缓存的正确使用方法(上)

    做项目的过程中,发现一次远程链接数据库的耗时大概是300ms~400ms,切身体会到了前辈们经常说的减少链接的重要性,用了缓存后页面的打开时间从1.5s减少到400ms 前提: 那么来说一说正题,we ...

  9. 条件锁condition与Queue()

    在学习之前你应该先了解锁和队列基础 import queue import time import random import threading import asyncio import logg ...

随机推荐

  1. 封装naive socket

    周五去一个公司打了个酱油,面试官问我:你封装过socket没? 言下之意是问我实际写过底层代码没,我悻悻地说写过点. PS:说实话木有封装过,今天无聊就来封装下. 话说写了这么久C++,底层用c来写还 ...

  2. vim缩进参考线

    编辑缩进嵌套的文件时想找到对应的层级比较困难,写了一个函数,使用cc选项设定一条辅助线,标识到指定的缩进层级.代码如下: ? ReferenceLine 1 2 3 4 5 6 7 8 9 10 11 ...

  3. 如何用STAF进行自动化测试分布式运行

    本文的目的在于引导读者去了解STAF及如何调用其接口去实现自动化测试的分布式动行. 提到分布式运行,很多人想到了Jenkins,Jenkins里面有个node插件,可以去分派任务给slave,Jenk ...

  4. C# winform DataGridView 常见属性

    C# winform DataGridView 属性说明① 取得或者修改当前单元格的内容 ② 设定单元格只读 ③ 不显示最下面的新行 ④ 判断新增行 ⑤ 行的用户删除操作的自定义 ⑥ 行.列的隐藏和删 ...

  5. Entity Framework中实体模型命名空间的问题

    在添加一个实体数据模型的时候(就是扩展名为edmx那位)属性里明明设置了自己的命名空间,但是后台的Designer.cs命名空间生成规则却是项目的默认命名空间加edmx文件所在文件夹的名称, 是BUG ...

  6. window 10 企业版激活

    一. 用管理员权限打开CMD.EXE 接着输入以下命令: slmgr /ipk NPPR9-FWDCX-D2C8J-H872K-2YT43 弹出窗口提示:“成功的安装了产品密钥”. 继续输入以下命令: ...

  7. Eclipse中Ant的配置与测试

    在Eclipse中使用Ant Ant是Java平台下非常棒的批处理命令执行程序,能非常方便地自动完成编译,测试,打包,部署等等一系列任务,大大提高开发效率.如果你现在还没有开始使用Ant,那就要赶快开 ...

  8. MongoDB学习笔记(11)-- Index

    索引通常能够极大的提高查询的效率,如果没有索引,MongoDB在读取数据时必须扫描集合中的每个文件并选取那些符合查询条件的记录. 这种扫描全集合的查询效率是非常低的,特别在处理大量的数据时,查询可以要 ...

  9. android安卓系统上运行jar文件

    原文链接: http://blog.sina.com.cn/s/blog_658c8cea0101mdhp.html 步骤如下: 1. 将文件打包成可执行jar文件(可在eclipse里export) ...

  10. AndroidUI设计 之 图片浏览器

    图片浏览器效果图 : 源码下载地址 : -- CSDN : http://download.csdn.net/detail/han1202012/6875083 -- GitHub : https:/ ...