Condition Variables(条件变量)用法指南

简介:

    int pthread_cond_timedwait(pthread_cond_t *restrict cond,

              pthread_mutex_t *restrict mutex,const struct timespec *restrict abstime);

      int pthread_cond_wait(pthread_cond_t *restrict cond,

              pthread_mutex_t *restrict mutex);

The pthread_cond_timedwait() and pthread_cond_wait() functions shall block on a condition variable. They shall be called with mutex locked by the calling thread or undefined behavior results.

These functions atomically release mutex and cause the calling thread to block on the condition variable cond; Upon successful return, the mutex shall have been locked and shall be owned by the calling thread. (State change: lock=> unlock and block => unblock and lock)

NOTE(condition variable and mutex): The effect of using more than one mutex for concurrent pthread_cond_timedwait() or  pthread_cond_wait() operations on the same condition variable is undefined.

Condition variable and thread cancellation

 A condition wait (whether timed or not) is a cancellation point. When the cancelability enable state of a thread is set to PTHREAD_CANCEL_DEFERRED, a side effect of acting upon a cancellation request while in a condition wait is that the mutex is (in effect) re-acquired before calling the first cancellation cleanup handler.

NOTE: if the mutex should be unlocked after thread cancellation, then the first cancellation cleanup handler is used to unlock mutex generally!

A  cancelled thread, which has  been  unblocked because  it  has been  canceled  while blocked  in  a  call  to pthread_cond_timedwait()  or  pthread_cond_wait(),  shall  not consume any condition signal.

Condition variable and signal

If a signal is delivered to a thread waiting for a condition variable, upon return from the signal handler the thread resumes waiting for the condition variableas if it was not interrupted, or it shall return zero due to spurious wakeup.

NOTE: It means that signal won’t block cond variable state!

Genaral Uasge

In general, whenever a condition wait returns, the thread has to re-evaluate the predicate associated with the condition waiting to determine whether it can safely proceed. A return from the wait does not imply that the associated predicate is either true or false. It is thus recommended that acondition wait be enclosed in the equivalent of a "while loop" thatchecks the predicate.

 

       int pthread_cond_broadcast(pthread_cond_t*cond);

       int pthread_cond_signal(pthread_cond_t *cond);

The pthread_cond_broadcast() function shall unblock all threads currently blocked on the specified condition variable cond.

The pthread_cond_signal() function shall unblock at least one of the threads that  are  blocked on  the specified condition variable cond

NOTE(waitup and mutex):The pthread_cond_broadcast() or pthread_cond_signal() functions may be called by a thread whether or not it  currently  owns the mutex that threads calling pthread_cond_wait() or pthread_cond_timedwait() have associated with the condition variable during their waits; however, if predictable  scheduling behavior is  required,  then that  mutex  shall be  locked  by the  thread calling pthread_cond_broadcast() or pthread_cond_signal().(The condition variable will be unblock but wait for mutex.)

The pthread_cond_broadcast() and pthread_cond_signal() functions shall have no effect if there  are no threads currently blocked on cond.(It means multi calls is safe!)

Condition variable and signal handler

It is not safe to use the pthread_cond_signal() function in a signal  handler that  is  invoked asynchronously.(It means pthread_cond_signal() shouldn’t be called in signal handler!)

Mutexes and condition variables are thus not suitable for releasing a waiting thread by signaling from code running in a signal handler.(Mutex and cond shouldn’t be used together with signal handler! IT IS NOT SAFE!)

Example:

---------------------------------------------------

兄弟的公司:立即购--手机购物,诚信网购

兄弟的公司:立即团

欢迎转载,请注明作者和出处


本文转自 zhenjing 博客园博客,原文链接: http://www.cnblogs.com/zhenjing/archive/2010/12/30/Condition_variable.html  ,如需转载请自行联系原作者

相关文章
|
8月前
|
人工智能 监控 安全
HiClaw 加入 AgentScope,携手 CoPaw 共建多 Agent 的基础设施
近日,HiClaw GitHub 仓库迁移至 AgentScope 下,将携手 CoPaw 共建多 Agent 的基础设施。
845 59
|
2月前
|
人工智能 自然语言处理 安全
阿里云百炼产品月报【2026年9月】
阿里云百炼本月重磅升级:Qwen3.8全模态实时模型上线,Token Plan取消周限额、新增Essential套餐及Agent Harness工具权益;Flow Agent预置模板即开即用,MCP广场上新46项服务,覆盖科研、金融、多媒体等场景;应用与Skill广场新增超30款模板及解决方案,控制台全面焕新,助力企业高效构建AI应用。
1073 0
|
4月前
|
人工智能 开发工具 数据库
基于阿里云 Milvus 构建短剧生产平台--生图生视频与内容搜索实践
本文用四个 Milvus AI Function 把 AIGC 短剧生产的完整链路串成一条「生成 → 检索」流水线,它的价值不只是"生成快",更在于把每一次生成都沉淀为可检索、可复用的素材资产,让内容团队从一次性消耗走向资产化经营。
|
弹性计算 运维 安全
毅硕HPC | Pritunl + ECS + Frp 搭建远程办公VPN
本教程将介绍一种 “鱼与熊掌兼得” 的创新方案:核心VPN服务(Pritunl)部署在您本地的强大工作站上,通过轻量级的FRP内网穿透工具,借助阿里云ECS提供的稳定公网入口,安全地将其服务能力发布到互联网。 这样,远程员工只需连接到一个统一的云上地址,即可直接接入您办公室或实验室的本地网络,访问其中的设备,实现真正意义上的“远程局域网”体验。
1444 1
毅硕HPC | Pritunl + ECS + Frp 搭建远程办公VPN
|
9月前
|
数据采集 SQL 人工智能
不会写代码,也能用AI做数据分析?手把手教你
本文介绍如何用AI(如ChatGPT或文心一言、通义千问等)零代码完成日常数据分析:上传Excel,用自然语言提问,即可自动清洗数据、发现异常、计算指标、生成图表与汇报文案,大幅提升运营、销售、产品等岗位的数据处理效率。
|
8月前
|
人工智能 安全 测试技术
全栈开发者必备:AI全流程研发实战技巧与案例分享全栈开发者必备
很多开发者对AI编程的印象还停留在写片段、补代码,但真正落地到团队项目、需求评审、架构设计、Code Review全链路时,大多AI都显得“水土不服”。最近深度实践了AI全流程研发模式,结合行业实践与真实项目落地,聊一聊如何把AI从“辅助写代码”变成覆盖需求→设计→开发→审查的工程化研发助力。
1779 3
|
12月前
|
API Docker 异构计算
从 Docker Run 到生产级部署:vLLM推理大模型服务的 Docker Compose 完整实战指南
本文详解如何将vLLM大模型推理服务从基础docker run升级为生产级Docker Compose部署。涵盖GPU支持、网络隔离、配置参数化、API优化与压测实践,解决命名冲突、环境混乱等痛点,助力AI/DevOps工程师构建可维护、可扩展的容器化推理平台。
|
架构师 IDE Java
【Java架构师】Maven中lombok那点事
SpringBoot项目中Lombok需在maven-compiler-plugin中配置`annotationProcessorPaths`,确保编译期生成getter/setter等方法;而`excludes`则在打包时排除Lombok依赖,减小体积,因运行时已无需该库。
1075 1
|
安全 Android开发 iOS开发
Android vs. iOS:构建生态差异与技术较量的深度剖析###
本文深入探讨了Android与iOS两大移动操作系统在构建生态系统上的差异,揭示了它们各自的技术优势及面临的挑战。通过对比分析两者的开放性、用户体验、安全性及市场策略,本文旨在揭示这些差异如何塑造了当今智能手机市场的竞争格局,为开发者和用户提供决策参考。 ###
|
数据采集 自然语言处理 搜索推荐
基于qwen2.5的长文本解析、数据预测与趋势分析、代码生成能力赋能esg报告分析
Qwen2.5是一款强大的生成式预训练语言模型,擅长自然语言理解和生成,支持长文本解析、数据预测、代码生成等复杂任务。Qwen-Long作为其变体,专为长上下文场景优化,适用于大型文档处理、知识图谱构建等。Qwen2.5在ESG报告解析、多Agent协作、数学模型生成等方面表现出色,提供灵活且高效的解决方案。
1800 49

热门文章

最新文章