zoukankan      html  css  js  c++  java
  • 重读APUE(15)-pthread_cond_wait与while循环

    即使pthead_cond_wait()和pthread_cond_timewait()没有错误返回,等待的条件也可能是假的;即使pthread_cond_timewait()返回了超时错误,关联的条件也可能是真的;因为超时和条件改变之间有时间窗口;

    在有些实现中,特别是多处理器环境,信号唤醒可能会同时唤醒多个处理器上的多个线程;

    通常,不管pthread_cond_wait()返回什么,都要对条件进行重新判断,以确定下一步动作,或者继续执行,或者继续等待;

    因此,推荐使用while循环来对等待条件进行检查;

    以下是pthread_cond_wait的man手册内容截取;

    It is important to note that when pthread_cond_wait() and pthread_cond_timedwait() return without error, the associated predicate may still be false. Sim‐
    ilarly, when pthread_cond_timedwait() returns with the timeout error, the associated predicate may be true due to an unavoidable race between the expira‐
    tion of the timeout and the predicate state change.

    Some implementations, particularly on a multi-processor, may sometimes cause multiple threads to wake up when the condition variable is signaled simulta‐
    neously on different processors.

    In general, whenever a condition wait returns, the thread has to re-evaluate the predicate associated with the condition wait to determine whether it can
    safely proceed, should wait again, or should declare a timeout. A return from the wait does not imply that the associated predicate is either true or false.

    It is thus recommended that a condition wait be enclosed in the equivalent of a “while loop” that checks the predicate.

  • 相关阅读:
    google
    html页面清除缓存
    EF中使用SQL语句或存储过程
    在存储过程中编写正确的事务处理代码
    .Net中Math.Round与四舍五入
    WebService中实现上传下载文件
    tony_linux下网站压力测试工具webbench
    把Nginx加为系统服务(service nginx start/stop/restart)
    Nginx+Tomcat+Keepalived+Memcache 负载均衡动静分离技术
    python学习笔记(1)--遍历txt文件,正则匹配替换文字
  • 原文地址:https://www.cnblogs.com/wanpengcoder/p/11764904.html
Copyright © 2011-2022 走看看