对pthread_cond_wait()函数的理解(我在CU上回复一个人的问题的解答) (个人见解,如有错误,恳请大家指出) /************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. 上面是APUE的原话,就是说pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex)函数传入的参数mutex用于保护条件,因为我们在调用pthread_cond_wait时,如果条件不成立我们就进入阻塞,但是进入阻 塞这个期间,如果条件变量改变了的话,那我们就漏掉了这个条件。因为这个线程还没有放到等待队列上,所以调用pthread_cond_wait前要先锁 互斥量,即调用pthread_mutex_lock(),pthread_cond_wait在把线程放进阻塞队列后,自动对mutex进行解锁,使得 其它线程可以获得加锁的权利。这样其它线程才能对临界资源进行访问并在适当的时候唤醒这个阻塞的进程。当pthread_cond_wait返回的时候又自动给mutex加锁。 实际上边代码的加解锁过程如下: /************pthread_cond_wait()的使用方法**********/ pthread_mutex_lock(&qlock);    /*lock*/ pthread_cond_wait(&qready, &qlock); /*block-->unlock-->wait() return-->lock*/ pthread_mutex_unlock(&qlock); /*unlock*/ /*****************************************************/

转载于:https://www.cnblogs.com/hdjsjlbs/p/3324332.html

对pthread_cond_wait()函数的理解相关推荐

  1. javascript 匿名函数的理解

    代码如下: (function(){ //这里忽略jQuery所有实现 })(); (function(){ //这里忽略jQuery所有实现 })(); 半年前初次接触jQuery的时候,我也像其他 ...

  2. pthread_cond_wait() 函数的使用

    1. 首先pthread_cond_wait 的定义是这样的 The pthread_cond_wait() and pthread_cond_timedwait() functions are us ...

  3. 如何用数学函数去理解机器学习?

    本文主要分享了如何基于数学函数原理去理解机器学习的本质,并简要介绍了机器学习的过程. 近期也是在做项目的过程中发现,其实AI产品经理不需要深入研究每一种算法,能了解机器学习的过程,这其中用到哪些常用算 ...

  4. Java 回调函数的理解

    以下是我对java回调函数的理解,希望对各位有帮助. 简而言之,假设有两个类A与B,还有一个回调接口C(有一个c方法).其中A类持有一个B类对象作为属性和拥有一个a方法,并实现了接口C,所以A类中就有 ...

  5. 7.STM32中对DMA_Config()函数的理解(自定义)测试DMA传输数据时CPU还可继续工作其他的事

    STM32中对DMA_Config()函数的理解(自定义):

  6. 4.STM32中对USART1_Config()函数的理解(自定义)

    STM32中对USART1_Config()函数的理解

  7. 3.STM32中对EXTI_PE5_Config()函数的理解(自定义)之中断控制按键LED

    STM32中对EXTI_PE5_Config()函数的理解(自定义)

  8. 2.STM32中对Key_GPIO_Config()函数的理解(自定义)之轮询控制按键LED

    STM32中对Key_GPIO_Config()函数的理解(自定义)

  9. 1.STM32中对LED_GPIO_Config()函数的理解(自定义)之流水灯

    STM32中对LED_GPIO_Config()函数的理解(自定义)

最新文章

  1. 用Ext-4.2简单实现分页效果
  2. c 语言 文本处理范例
  3. 论文浅尝 | IRW:基于知识图谱和关系推理的视觉叙事框架
  4. python编程学习_使用EduBlocks轻松学习Python编程
  5. 笨办法学 Python · 续 第二部分:简单的黑魔法
  6. 吴恩达深度学习(一)-第三周:Planar data classification with one hidden layer
  7. php 读取管道,php – 使用proc_open时从STDIN管道读取
  8. Tomcat端口占用的处理方式
  9. 阿里大牛程序员的Java问题排查工具单
  10. 阿里云存储掌门人吴结生:微秒存储时代,阿里云凭什么领跑!
  11. C语言的红外计数程序,51单片机红外计数器 电路原理图+PCB+源程序等资料分享
  12. 择校秘籍|南大计算机 和 北航计算机 应该怎么选?
  13. 光学图像、SAR图像等区别
  14. Vue 2.x折腾记 - (15) 捣鼓一个中规中矩loading组件
  15. 计算机安装win10配置,win11发布了,那么安装win11配置要求是什么?win11配置要求详解...
  16. gcrobustvar:基于VAR的稳健性Granger因果检验
  17. IDEA Diagrams查看类图关系太乱
  18. Android 报错Failed to load native library: XXXX_so
  19. IT培训与超级实习生怎么选呢?都是1W+费用
  20. iphone忘记密码了怎么开锁

热门文章

  1. java获取接口数据类型_java中调用第三方接口获取数据的方式
  2. Fatal Error: Unable to find package java.lang in classpath or bootclasspath
  3. html filter 作用,css滤镜有什么作用?
  4. WebMvcConfigurer
  5. Java防止Xss注入json_浅谈 React 中的 XSS 攻击
  6. jmeter对乱码如何处理_JMeter中文乱码的解决
  7. python3.4.4怎么用_python3.4使用文件
  8. 再也不怕别人动电脑了!用Python实时监控
  9. Python 的 51 个秘密曝光,Github 获 2 万星
  10. bootstrap中表格大小设置_bootstrap 设置table - td宽度问题