- 0
- 0
- 约1.94万字
- 约 12页
- 2023-04-25 发布于上海
- 举报
Disclaimer :
Disclaimer : The original version of this article was first published on IBM developerWorks, and is property of Westtech Information Services. This document is an updated version of the original article, and contains various improvements made by the Gentoo Linux Documentation team.
This document is not actively maintained.
POSIX threads explained, part 3
Content:
Improve efficiency with condition variables Condition variables explained
I ended my previous article by describing a particular dilemma how does a thread deal with a situation where it is waiting for a specific condition to become true? It could repeatedly lock and unlock a mutex, each time checking a shared data structure for a certain value. But this is a waste of time and resources, and this form of busy polling is extremely inefficient. The best way to do this is to use the pthread_cond_wait() call to wait on a particular condition to become true.
Its important to understand what pthread_cond_wait() does -- its the heart of the POSIX threads signalling system, and also the hardest part to understand.
First, lets consider a scenario where a thread has locked a mutex, in order to take a look at a linked list, and the list happens to be empty. This particular thread cant do anything -- its designed to remove a node from the list, and there are no nodes available. So, this is what it does.
While still holding the mutex lock, our thread will call pthread_cond_wait(mycond,mymutex). The pthread_cond_wait() call is rather complex, so well step through each of its operations one at a time.
The first thing pthread_cond_wait() does is simultaneously unlock the mutex mymutex (so that other threads can modify the linked list) and wait on the condition mycond (so that pthread_cond_wait() will wake up when it is signalled by another thread). Now that the mutex is unlocked, other threads can access and modify the linked list, possibly adding items.
At this point, the pthread_cond_wait() call has not yet
您可能关注的文档
- 20000字起笔部首检字表.docx
- 20148小继教培训心得体会.docx
- 20151人教版三年级数学上册教学工作总结.docx
- 20165理论题分析和总结.docx
- 20192广东公需课 中华传统美德.docx
- 22018年黄浦区中考物理二模卷含答案.docx
- 070602EditPlus安装步骤分析和总结.docx
- 100128万达国际时报文案.docx
- 100413晨报万达软文.docx
- 00192006中国软件企业出口20强等.docx
- 中国国家标准 GB/Z 37551.300-2026海洋能 波浪能、潮流能及其他水流能转换装置 第300部分:河流能转换装置发电性能评估.pdf
- GB/T 44937.3-2025集成电路 电磁发射测量 第3部分:辐射发射测量 表面扫描法.pdf
- 中国国家标准 GB/T 44937.3-2025集成电路 电磁发射测量 第3部分:辐射发射测量 表面扫描法.pdf
- 《GB/T 44937.3-2025集成电路 电磁发射测量 第3部分:辐射发射测量 表面扫描法》.pdf
- 中国国家标准 GB/T 44937.1-2025集成电路 电磁发射测量 第1部分:通用条件和定义.pdf
- GB/T 44937.1-2025集成电路 电磁发射测量 第1部分:通用条件和定义.pdf
- 《GB/T 44937.1-2025集成电路 电磁发射测量 第1部分:通用条件和定义》.pdf
- 中国国家标准 GB/T 4937.37-2025半导体器件 机械和气候试验方法 第37部分:采用加速度计的板级跌落试验方法.pdf
- 《GB/T 4937.10-2025半导体器件 机械和气候试验方法 第10部分:机械冲击 器件和组件》.pdf
- 中国国家标准 GB/T 44937.2-2025集成电路 电磁发射测量 第2部分:辐射发射测量TEM小室和宽带TEM小室法.pdf
最近下载
- 山东省春季高考语文真题含答案(打印).docx VIP
- JJF 166 4-2017_温度显示仪校准规范.pdf VIP
- 北京林业大学719园林植物历年考研真题(2000-2012、2015、2017-2020).pdf VIP
- 文献翻译-早年客体关系中的皮肤体验(王蕾译).docx VIP
- 网渔网咖网鱼员工手册.pdf VIP
- 汉语口语速成入门篇上第4课:多少钱教程.pptx VIP
- 北师大版五年级下册数学竞赛试题.pdf VIP
- xflow应用培训教程9多孔介质.pptx VIP
- 部编版语文一年级下册-语文园地八.docx VIP
- 港张家港港区化学工业园作业区孚宝化工码头二期工程环境影响评估.doc VIP
原创力文档

文档评论(0)