注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

还东国的博客

行之苟有恒,久久自芬芳

 
 
 

日志

 
 

C++11及BOOST特性之四condition_variable的wait  

2014-01-06 16:10:11|  分类: C++(VC)编程 |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |

C++11BOOST特性之四condition_variablewait

这里对这个wait有一个不太明白的认识,今天调试程序,发现了一个情况,也就是在:

XXX函数

{      

std::unique_lock<std::mutex> uniqueLocker(_mutex);

 

         //防止假唤醒,但会影响效率

         while (!this->_flag)

         {

                   this->_conVar.wait(uniqueLocker);

         }

}

上面这种情况,多个线程都可以进入WAIT,也就是说前的锁根本没有锁住。很奇怪,如果直接只是用一个锁,就可以锁信也就是说把wait那一行去除或注释。查了下资料:

std::condition_variable::wait

unconditional (1)   

void wait (unique_lock<mutex>& lck);

predicate (2)  

template <class Predicate>

  void wait (unique_lock<mutex>& lck, Predicate pred);

Wait until notified

The execution of the current thread (which shall have locked lck's mutex) is blocked until notified.

 

At the moment of blocking the thread, the function automatically calls lck.unlock(), allowing other locked threads to continue.

 

Once notified (explicitly, by some other thread), the function unblocks and calls lck.lock(), leaving lck in the same state as when the function was called. Then the function returns (notice that this last mutex locking may block again the thread before returning).

 

Generally, the function is notified to wake up by a call in another thread either to member notify_one or to member notify_all. But certain implementations may produce spurious wake-up calls without any of these functions being called. Therefore, users of this function shall ensure their condition for resumption is met.

 

If pred is specified (2), the function only blocks if pred returns false, and notifications can only unblock the thread when it becomes true (which is specially useful to check against spurious wake-up calls). This version (2) behaves as if implemented as:

 

while (!pred()) wait(lck);

看红色部分,原来真是这个竞态条件做得好事,他在阻塞住线程后直接就调用unlock,目的是为了让别的线程也可以操作。太可怕了。

回头再翻看pthread_cond_t,他的wait和上面的也一样。

/************pthread_cond_wait()的使用方法**********/

    pthread_mutex_lock(&qlock);   

    pthread_cond_wait(&qready, &qlock);

    pthread_mutex_unlock(&qlock);

/*****************************************************/

The mutex passed to pthread_cond_wait protects the condition.The caller

passes it locked to the function, which then atomically places the

calling thread on the list of threads waiting for the condition and

unlocks the mutex. This closes the window between the time that the

condition is checked and the time that the thread goes to sleep waiting

for the condition to change, so that the thread doesn't miss a change

in the condition. When pthread_cond_wait returns, the mutex is again

locked.

跟标准库的用法一致。看来他们两个是穿一条裤子的啊。

凡事都怕认真二字,果然如此。

  评论这张
 
阅读(912)| 评论(2)
推荐 转载

历史上的今天

在LOFTER的更多文章

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2017