pthread_cond_signal()的具体位置?
"pthread_cond_signal()必须要放在pthread_mutex_lock() 和pthread_mutex_unlock() 之间, "
我认为这个做法有个问题,举个例子 简单假设线程1、,curnum 值为 , 语句执行顺序如下: T2-->;pthread_mutex_lock(&mutex_curnum);
T2-->;while(curnum)
T2-->; pthread_cond_wait(&cond_curnum,&mutex_curnum);/*T2解锁,睡眠,等信号*/
T1-->;pthread_mutex_lock(&mutex_curnum); /*轮T1运行,T1上锁*/
T1-->;if (!-curnum) /*条件成立*/
T1-->; pthread_cond_signal(&cond_curnum); /*T1向线程T2发信号*/
T2-->;pthread_cond_wait(&cond_curnum,&mutex_curnum); /*T1时间片用完,换T2执行,但发觉不能上锁,因为T1持有锁*/
T1-->;pthread_mutex_unlock(&mutex_curnum); /*T1解锁*/ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
问题在于一个条件变量和一个互斥锁关联,
一个线程在持有锁的情况下调用pthread_cond_signal(),
则等待条件的线程有可能得不到锁。 就上面的特定例子来,配合每条语句的执行顺序,
虽然T1调用了pthread_cond_signal(), 但是T2 %不能获得锁。 《UNIX网络编程第2卷:进程间通信》提了一个改进方法
pthread_mutex_lock();
判断是否别个线程等待的条件发生,是的话设 "发生标志" 为 "是";
pthread_mutex_unlock();
if (发生标志 == 是)
{
pthread_cond_signal(...);
}
例子如下:
shows an example of how to use condition variables and mutexes together to synchronize threads.
The condition is the state of the work queue. We protect the condition with a mutex and evaluate the condition in a while loop. When we put a message on the work queue, we need to hold the mutex, but we don't need to hold the mutex when we signal the waiting threads. As long as it is okay for a thread to pull the message off the queue before we call cond_signal, we can do this after releasing the mutex. Since we check the condition in a while loop, this doesn't present a problem: a thread will wake up, find that the queue is still empty, and go back to waiting again. If the code couldn't tolerate this race, we would need to hold the mutex when we signal the threads.
gure 11.14. Using condition variables
#include <pthread.h> struct msg {
struct msg *m_next;
/* ... more stuff here ... */
};
struct msg *workq;
pthread_cond_t qready = PTHREAD_COND_INITIALIZER;
pthread_mutex_t qlock = PTHREAD_MUTEX_INITIALIZER; void
process_msg(void)
{
struct msg *mp; for (;;) {
pthread_mutex_lock(&qlock);
while (workq == NULL)
pthread_cond_wait(&qready, &qlock);
mp = workq;
workq = mp->m_next;
pthread_mutex_unlock(&qlock);
/* now process the message mp */
}
} void
enqueue_msg(struct msg *mp)
{
pthread_mutex_lock(&qlock);
mp->m_next = workq;
workq = mp;
pthread_mutex_unlock(&qlock);
pthread_cond_signal(&qready);
} pthread_cond_wait必须放在pthread_mutex_lock和pthread_mutex_unlock之间,
因为他要根据共享变量的状态来觉得是否要等待,而为了不永远等待下去所以必须要在lock/unlock队中
共享变量的状态改变必须遵守lock/unlock的规则 pthread_cond_signal即可以放在pthread_mutex_lock和pthread_mutex_unlock之间,
也可以放在pthread_mutex_lock和pthread_mutex_unlock之后,但是各有有缺点。 之间:
pthread_mutex_lock
xxxxxxx
pthread_cond_signal
pthread_mutex_unlock
缺点:在某下线程的实现中,会造成等待线程从内核中唤醒(由于cond_signal)然后又回到内核空间
(因为cond_wait返回后会有原子加锁的行为),所以一来一回会有性能的问题。
但是在LinuxThreads或者NPTL里面,就不会有这个问题,因为在Linux 线程中,
有两个队列,分别是cond_wait队列和mutex_lock队列, cond_signal只是
让线程从cond_wait队列移到mutex_lock队列,而不用返回到用户空间,不会有性能的损耗。
所以在Linux中推荐使用这种模式。 之后:
pthread_mutex_lock
xxxxxxx
pthread_mutex_unlock
pthread_cond_signal
优点:不会出现之前说的那个潜在的性能损耗,因为在signal之前就已经释放锁了
缺点:如果unlock在signal之前,有个低优先级的线程正在mutex上等待的话,
那么这个低优先级的线程就会抢占高优先级的线程(cond_wait的线程),
而这在上面的放中间的模式下是不会出现的。 所以,在Linux下最好pthread_cond_signal放中间,但从编程规则上说,其他两种都可以
(但我认为pthread_cond_signal放中间那样是不可以的)
转载自:http://blog.csdn.net/dove1984/article/details/7996085

pthread_cond_signal该放在什么地方?的更多相关文章

  1. js脚本都可以放在哪些地方

    js脚本应该放在页面的什么地方 1.head部分 包含函数的脚本位于文档的 head 部分.这样我们就可以确保在调用函数前,脚本已经载入了. 2.body部分 执行位于 body 部分的脚本. 3.外 ...

  2. Mac下Homebrew安装的软件放在什么地方

    一般情况是这么操作的: 1.通过brew install安装应用最先是放在/usr/local/Cellar/目录下. 2.有些应用会自动创建软链接放在/usr/bin或者/usr/sbin,同时也会 ...

  3. 如何查看yum安装的程序包都放在哪些地方了?

    yum安装, 是先下载下来, 然后安装, 用dnf代替yum后, 配置文件跟yum类似, 也是放在 /etc/dnf/ 目录下的, 也有dnf.conf 配置文件等 dnf list后面还可以跟参数: ...

  4. 开发人员的福音:微软、谷歌、Mozilla将他们所有的web API文档放在同一个地方

    Tips 原文作者:Liam Tung  原文地址:Developers rejoice: Microsoft, Google, Mozilla are putting all their web A ...

  5. 【python驱动】python进行selenium测试时GeckoDriver放在什么地方?

    背景:用python进行selenium 关于b/s架构的测试,需要配置驱动否则程序无法执行 情况1:windows下放置GeckoDriver 步骤1:下载驱动 GeckoDriver下载地址fir ...

  6. APP的缓存文件放在哪里?

    只要是需要进行联网获取数据的APP,都会在本地产生缓存文件.那么,这些缓存文件到底放在什地方合适呢?系统有没有给我们提供建议的缓存位置呢?不同的缓存位置有什么不同呢? 考虑到卸载APP必须删除缓存 在 ...

  7. jsp文件放在WebRoot下还是WebInfo下

    观点一:(较为赞同) 安全性不是真正的原因,因为jsp是要解析后才显示到浏览器的,即使用户知道你jsp的路径,也不可能通过浏览器看到jsp源码的,而如果是通过其它手段入侵服务器的话,放在WEB-INF ...

  8. Linux 江湖系列阶段性总结

    引言 我使用 Linux 已经有很多年了,最开始接触 Linux 的时候是从 RedHat 9(没有 Enterprise),中途换过 N 个不同的发行版.多年前,我在 BlogJava 上面分享 J ...

  9. 代码的坏味道(18)——依恋情结(Feature Envy)

    坏味道--依恋情结(Feature Envy) 特征 一个函数访问其它对象的数据比访问自己的数据更多. 问题原因 这种气味可能发生在字段移动到数据类之后.如果是这种情况,你可能想将数据类的操作移动到这 ...

随机推荐

  1. linux下修改文件权限

    加入-R 参数,就可以将读写权限传递给子文件夹例如chmod -R 777 /home/mypackage那么mypackage 文件夹和它下面的所有子文件夹的属性都变成了777777是读.写.执行权 ...

  2. vi/vim 查找替换使用方法

    http://wzgyantai.blogbus.com/logs/28117977.html vi/vim 中可以使用 :s 命令来替换字符串.该命令有很多种不同细节使用方法,可以实现复杂的功能,记 ...

  3. PMP备考经验分享

    大学毕业到现在工作已经5年了,在最初的3年里一直从事软件开发的工作,但是在后面的工作时间里,接触到了项目管理,不仅 需要自己能做,而且要带领团队做,管控项目了.当时部门里的技术经理学习过PMP,并参加 ...

  4. Python练习笔记——字符串反转

    请输入一段字符串,不利用反转函数,编写一段代码,将其反转. def list_reverse(a): list_long = len(a) list_long_half = list_long // ...

  5. JMeter学习笔记--详解JMeter配置元件

    JMeter配置元件可以用来初始化默认值和变量,以便后续采样器使用.将在其作用域的初始化阶段处理. CSV Data Set Config:被用来从文件中读取数据,并将它们拆分后存储到变量中,适合处理 ...

  6. USB协议及认知

    1.USB的拓扑结构决定了主机控制器就是最高统帅,没有主机控制器的要求设备永远不能主动发数据.所以主机控制器在USB 的世界里扮演着重要的角色,它是幕后操纵者. 2.数据包的发送, 这个过程包含很多信 ...

  7. Nginx+mysql+php(待补全)

    先安装 Nginx 通过源码安装可以指定目录(/usr/local/Nginx/) yum或源码安装Mysql yum或源码安装php(记得命令一定要全,别忘了安装php-fpm) 待补全

  8. texturePacker黄色文件夹和蓝色文件夹

    texturePacker中以黄色文件夹添进来的文件夹中的图片,不用带文件夹名,以蓝色文件夹添进来的文件要带文件夹名.

  9. Spring中三种配置Bean的方式

    Spring中三种配置Bean的方式分别是: 基于XML的配置方式 基于注解的配置方式 基于Java类的配置方式 一.基于XML的配置 这个很简单,所以如何使用就略掉. 二.基于注解的配置 Sprin ...

  10. Android中的httpclient框架发送get请求

    /** * 採用httpclientGet请求的方式 * * @param username * @param password * @return null表示求得的路径有问题,text返回请求得到 ...