线程条件变量详解

线程间通信:pthread条件变量详解 阅读详情

函数:

pthread_cond_init, pthread_cond_destroy, pthread_cond_signal,

pthread_cond_broadcast, pthread_cond_wait, pthread_cond_timedwait

概要:

#include <pthread.h>

pthread_cond_t cond = PTHREAD_COND_INITIALIZER;

int pthread_cond_init(pthread_cond_t *cond, pthread_condattr_t *cond_attr);

int pthread_cond_signal(pthread_cond_t *cond);

int pthread_cond_broadcast(pthread_cond_t *cond);

int pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex);

int pthread_cond_timedwait(pthread_cond_t *cond, pthread_mutex_t *mutex, const struct timespec *abstime);

int pthread_cond_destroy(pthread_cond_t *cond);

 

描述:

A condition (short for ``condition variable'') is a synchronization

device that allows threads to suspend execution and relinquish the pro-

cessors until some predicate on shared data is satisfied. The basic

operations on conditions are: signal the condition (when the predicate

becomes true), and wait for the condition, suspending the thread execu-

tion until another thread signals the condition.


A condition variable must always be associated with a mutex, to avoid

the race condition where a thread prepares to wait on a condition vari-

able and another thread signals the condition just before the first

thread actually waits on it.


pthread_cond_init initializes the condition variable cond, using the

condition attributes specified in cond_attr, or default attributes if

cond_attr is NULL. The LinuxThreads implementation supports no

attributes for conditions, hence the cond_attr parameter is actually

ignored.


Variables of type pthread_cond_t can also be initialized statically,

using the constant PTHREAD_COND_INITIALIZER.

pthread_cond_signal restarts one of the threads that are waiting on the

condition variable cond. If no threads are waiting on cond, nothing

happens. If several threads are waiting on cond, exactly one is

restarted, but it is not specified which.


pthread_cond_broadcast restarts all the threads that are waiting on the

condition variable cond. Nothing happens if no threads are waiting on

cond.


pthread_cond_wait atomically unlocks the mutex (as per

pthread_unlock_mutex) and waits for the condition variable cond to be

signaled. The thread execution is suspended and does not consume any

CPU time until the condition variable is signaled. The mutex must be

locked by the calling thread on entrance to pthread_cond_wait. Before

returning to the calling thread, pthread_cond_wait re-acquires mutex

(as per pthread_lock_mutex).


Unlocking the mutex and suspending on the condition variable is done

atomically. Thus, if all threads always acquire the mutex before sig-

naling the condition, this guarantees that the condition cannot be sig-

naled (and thus ignored) between the time a thread locks the mutex and

the time it waits on the condition variable.

(这段话说明之所以在调用pthread_cond_wait之前线程需要加锁,是因为从调用

 

pthread_cond_wait到进入condition wait的悬起状态之间的时间如果其他线程调用 pthread_cond_signal,这个signal将被忽略。为了不让这个signal被忽略,必须使调用 pthread_cond_wait到进入condition wait的悬起状态这段时间具有原子性,办法就是

在调用pthread_cond_wait和pthread_cond_signal之前都进行加锁,在线程进入

condition wait的悬起状态之前一刻释放锁 )


 

pthread_cond_timedwait atomically unlocks mutex and waits on cond, as

pthread_cond_wait does, but it also bounds the duration of the wait. If

cond has not been signaled within the amount of time specified by

abstime, the mutex mutex is re-acquired and pthread_cond_timedwait

returns the error ETIMEDOUT. The abstime parameter specifies an abso-

lute time, with the same origin as time(2) and gettimeofday(2); an abstime

of 0 corresponds to 00:00:00 GMT, January 1, 1970.


pthread_cond_destroy destroys a condition variable, freeing the

resources it might hold. No threads must be waiting on the condition

variable on entrance to pthread_cond_destroy. In the LinuxThreads

implementation, no resources are associated with condition variables,

thus pthread_cond_destroy actually does nothing except checking that

the condition has no waiting threads.

 

线程取消:

pthread_cond_wait and pthread_cond_timedwait are cancellation points.

If a thread is cancelled while suspended in one of these functions, the

thread immediately resumes execution, then locks again the mutex argu-

ment to pthread_cond_wait and pthread_cond_timedwait, and finally exe-

cutes the cancellation. Consequently, cleanup handlers are assured

that mutex is locked when they are called.

(pthread_cond_wait 和pthread_cond_timedwait都是线程可取消点,可以被 pthread_cancel取消,为了避免在pthread_cond_wait和pthread_mutex_cancel之间 被取消,可以使用线程清理函数处理,在清理函数中解锁 )

异步安全性:

The condition functions are not async-signal safe, and should not be

called from a signal handler. In particular, calling pthread_cond_sig-

nal or pthread_cond_broadcast from a signal handler may deadlock the

calling thread.

 

(条件变量不具备异步信号安全性,因此不要在信号处理函数中调用pthread_cond_signal 和pthread_cond_broadcast )

 

返回值:

All condition variable functions return 0 on success and a non-zero

error code on error.

错误

 

pthread_cond_init, pthread_cond_signal, pthread_cond_broadcast, and

pthread_cond_wait never return an error code.


The pthread_cond_timedwait function returns the following error codes

on error:

ETIMEDOUT

the condition variable was not signaled until the timeout

specified by abstime


EINTR pthread_cond_timedwait was interrupted by a signal

The pthread_cond_destroy function returns the following error code on

error:

EBUSY some threads are currently waiting on cond.

例子:

Consider two shared variables x and y, protected by the mutex mut, and

a condition variable cond that is to be signaled whenever x becomes

greater than y.

(为了避免其他因素激发condition wait状态的线程,可以使pthread_cond_wait 被激发仅仅成为必要条件,而while()中给出的条件才是我们等待的充分条件 )


 


Waiting until x is greater than y is performed as follows:


 


Modifications on x and y that may cause x to become greater than y

should signal the condition if needed:


 


If it can be proved that at most one waiting thread needs to be waken

up (for instance, if there are only two threads communicating through x

and y), pthread_cond_signal can be used as a slightly more efficient

alternative to pthread_cond_broadcast. In doubt, use

pthread_cond_broadcast.


To wait for x to becomes greater than y with a timeout of 5 seconds,

do:

























































SCL3400-D01双轴高精度倾角计 简介 两轴高精度加速度计,提供两个模式。 ±30° with 10 Hz LPF ±90° with 40 Hz LPF Resolution up to 32768 LSB/g Ultra-low 0.0009°/√Hz noise density 这款传感器是替换之前村田的停产的模拟量系列的芯片,正在测试中,具体效果大伙可以评测。 一、引脚连接图 引脚图兼容SCL3300 二、SPI接口 1.SCL3300和SCL3400略有区别如下图 左边为SCL3... 阅读详情

相关推荐

VCSA 6.7证书过期别慌!手把手教你通过SSH修改系统时间恢复登录(附STS证书修复脚本)

本文详细介绍了VCSA 6.7证书过期的应急处理方案,包括通过SSH修改系统时间临时恢复登录、全面更新证书体系以及专项修复STS证书的实战步骤。文章还提供了后期验证和系统加固的建议,帮助运维人员有效应对证书过期问题,确保虚拟化平台的稳定运行。

weixin_31292729的博客 415

ThreadLocal线程变量使用浅解

ThreadLocal线程变量使用浅解,希望能够给大家一点点帮助

皓月星辰 3004

自锁电路设计

Hi,uu们,是不是经常要用到自锁电路,通常不是使用555芯片就是用比较器来做自锁,今天我们来简单看下自锁电路的设计.图1采用了比较器构建了一个自锁电路,采用低电平复位,当需要复位的时候志需要将反向端的二极管拉低一下即可,免去 三极管控制同相端和电源短接的麻烦。图1:比较器构建的自锁电路也可以使用555去构建,如下图2所示.图2:使用555构建的自锁电路工作原理见555的真值表,真值表如表1所示.表1:555电路真值表相对来说555还是比较贵,所以我就想能不能有个便宜的方法做个自锁电路,构建电路如下图3所示

weixin_43884675的博客 7064

线程局部变量(ThreadLocal)

定义线程局部变量(ThreadLocal)是Java中的一个类,它允许我们为每个使用该变量的线程都提供一个变量值的副本,使得每个线程都可以独立地改变自己的副本,而不会影响到其他线程的副本。这种机制是通过在每个线程内部维护一个ThreadLocalMap来实现的,ThreadLocal对象作为这个Map的键,而线程局部变量的值则作为这个Map的值。原理:ThreadLocal内部通过ThreadLocalMap来存储每个线程的变量副本。

m0_70066267的博客 1804

ChatBI实战:如何用Vanna和WrenAI搭建企业级智能问数系统(附避坑指南)

本文详细介绍了如何利用Vanna和WrenAI搭建企业级智能问数系统(ChatBI),解决多源异构数据整合、语义理解准确率和系统性能安全等核心挑战。通过实战案例和优化策略,展示了这一组合方案在提升查询效率、实现业务自主分析和动态探索式分析方面的显著优势,特别适合企业级数据查询需求。

weixin_29047919的博客 399

华为OD机试真题 新系统【网络数据包收发处理】

华为OD机试 新系统真题【网络数据包收发处理】,提供C语言、Java、C++、Python、Go、JavaScript源码实现,并提供完整思路讲解,以及在线OJ模拟做题。华为OD机试真题【网络数据包收发处理】多语言题解。

qq_45776114的博客 444

ThreadLocal详解

ThreadLocal详解

Charge8的博客 6755

RK3588高速接口实战-PCIE

摘要: RK3588的PCIe接口支持PCIe 3.0x4和PCIe 2.1x1/x2,具备Root Complex和Endpoint模式,适用于NVMe SSD、5G模块等高速设备。硬件设计需注意85Ω/100Ω阻抗匹配、220nF AC耦合电容及100MHz参考时钟。设备树需配置控制器节点、PHY模式和IOMMU映射(RC模式用iommu-map,EP模式用iommus)。调试时可通过lspci检查链路状态,dmesg查看训练日志,perf分析性能。常见问题包括设备识别失败(检查复位时序、PHY配置)、

weiyvyy的博客 501

Threadlocal线程变量简介

ThreadLocal叫做线程变量,意思是ThreadLocal中填充的变量属于当前线程,该变量对其他线程而言是隔离的,也就是说该变量是当前线程独有的变量。ThreadLocal为变量在每个线程中都创建了一个副本,那么每个线程可以访问自己内部的副本变量。ThreadLoal 变量,线程局部变量,同一个 ThreadLocal 所包含的对象,在不同的 Thread 中有不同的副本。注意:每个 Thread 内有自己的实例副本,且该副本只能由当前 Thread 使用,故其它 Thread 不可访问,那就。

ShirleyZ1007的博客 884

HQChart实战教程21 - uniapp app端K线图定制tooltip

HQChart实战教程21 - uniapp app端K线图定制tooltip序效果步骤2. 注册标题绘制事件3. 标题绘制回调函数格式说明eventdataobjtooltip显示函数完整代码HQChart代码地址 序 hqchartK线图内置一个tooltip, 如果需要定制一个tooltip, 我们可以通过注册绘制标题事件,来自己做一个div显示数据。 参考教程HQChart实战教程7-自定义显示手势点击K线显示信息 效果 步骤 在外部创建一个view,并在view里创建需要显示的信息字段. 例子里

小银 2556

23种设计模式-代理(Proxy)设计模式

软考中级-软件设计师 代理设计模式

片言可决讼,半纸能缚龙 1783

一文读懂ThreadLocal「线程变量原理 源码剖析」

叫做线程变量,意思是中填充的变量属于当前线程,该变量对其他线程而言是隔离的,也就是说该变量是当前线程独有的变量。为变量在每个线程中都创建了一个副本,那么每个线程可以访问自己内部的副本变量。ThreadLoal变量,线程局部变量,同一个所包含的对象,在不同的Thread中有不同的副本。因为每个Thread内有自己的实例副本,且该副本只能由当前Thread使用。这是也是命名的由来。既然每个Thread有自己的实例副本,且其它Thread不可访问,那就不存在多线程间共享的问题。提供了线程本地的实例。

q787599931的博客 477

假人有限元模型

假人模型,车辆碰撞假人有限元模型,适合做乘员安全的研究者利用

Linux线程——条件变量(pthread_cond)详解

条件变量(Condition Variable)是线程同步的高级机制,它允许线程在特定条件不满足时主动让出CPU,直到其他线程改变条件并发出通知。条件变量总是与互斥锁配合使用,共同解决复杂的线程同步问题。与互斥锁绑定:必须与配合使用,避免竞争条件。线程阻塞与唤醒阻塞等待释放锁并挂起线程,直到被唤醒。唤醒机制:唤醒一个等待线程(由调度策略决定)。:唤醒所有等待线程。虚假唤醒:线程可能被意外唤醒(即使未收到信号),需用while循环检查条件。API功能描述关键参数返回值使用场景初始化条件变量。

weixin_45720999的博客 2101

STM32F407中文手册

STM32f407中文手册 本参考手册面向应用开发人员,提供有关使用 STM32F405xx/07xx、STM32F415xx/17xx、 STM32F42xxx 和 STM32F43xxx 微控制器存储器与外设的完整信息。STM32F405xx/07xx、STM32F415xx/17xx、STM32F42xxx 和 STM32F43xxx 构成一个微 控制器系列,各产品具有不同的存储器大小、封装和外设。有关订购信息以及器件的机械与电气特性,请参见数据手册。有关 ARM Cortex™-M4F 内核的信息,请参见《Cortex™-M4F 技术参考手册》。

复分析 阿尔福斯(Ahlfors) 第三版答案 英文

答案分为十章,有部分习题没有答案,全英文复分析阿尔福斯(Ahlfors) 第三版答案

Linux线程同步之条件变量详解

1.条件变量相关调用函数 #include<pthread.h> int pthread_cond_init(pthread_cond_t *restrict cond, const pthread_condattr *restrict attr); int pthread_cond_destroy(pthread_cond_t *cond); //这两个函数返回值:0成功,...

paradox_1_0的博客 366

VB摄像头监控

该VB模块可对摄像头捕获的画面进行录像等,可在创建的窗口进行控制

条件变量:多线程同步的核心机制详解

条件变量(Condition Variable)是多线程编程中实现线程间同步的核心机制。它允许线程在某个条件不满足时主动阻塞等待,当其他线程修改条件并发出通知时,再恢复执行。

嵌入式软件工程师养成记 1110

【Linux C | 多线程编程】线程同步 | 条件变量(万字详解)

本文介绍了Linux多线程编程中常见的条件变量,先是介绍了条件变量相关接口函数,然后介绍了怎么使用条件变量,最后给出了使用条件变量的例子。

wkd_007的博客 3424

多线程锁详解之【条件变量】

注:本文以windows临界区作为案例说明,其实各个操作系统中的临界锁的理论是相通的,源码逻辑也是大同小异,其中 linux 系统的 mutex 锁原理与逻辑可以说与windows临界区是一样的。 其次是很多开发者对锁的理解只局限于调用,如果需要他们自己实现一个锁,往往无能为力。比如XP系统没有条件变量和读写锁,当应用中需要的时候如何实现?还有如何模仿数据库中的表锁,行锁,网络锁,乐观锁等等。。。 理解临界区的底层原理与逻辑,是实现这些锁的第一条件! 正文开始先来回顾一下windows 临界区的几个函数:

qq492927689的博客 596

Linux条件变量&线程池详解

避免了多线程对同一块临界资源访问产生的冲突,同一时刻对临界资源的访问,,由此也带来了竞争的问题。即之间时刻都在这把锁,而临界资源是有限的,当的候,,反而降低了运行效率。有没有什么办法可以等生产者线程生产出资源,消费者线程再去竞争锁消费呢?这就是条件变量的作用。正如互斥量保护了【临界资源】,条件变量也保护【条件】资源,当条件不符合时即【条件】资源空缺,消费者线程休眠等待;当【条件】资源产生,消费者线程被唤醒然后去消费资源。这样便解决了线程间等待的问题,提高了运行效率。

2301_79532069的博客 863

linux 条件变量取值,Linux线程同步之条件变量详解

1.条件变量相关调用函数#includeint pthread_cond_init(pthread_cond_t *restrict cond, const pthread_condattr *restrict attr);int pthread_cond_destroy(pthread_cond_t *cond);//这两个函数返回值:0成功,否则返回错误编号int pthread_cond_w...

weixin_30310209的博客 134

Linux线程(十一)线程互斥锁-条件变量详解

条件变量是线程可用的另一种同步机制。条件变量用于自动阻塞线程,知道某个特定事件发生或某个条件满足为止,通常情况下,条件变量是和互斥锁一起搭配使用的。为了说明这个问题,来看一个没有使用条件变量的例子,生产者---消费者模式,生产者这边负责生产产品、而消费者负责消费产品,对于消费者来说,没有产品的时候只能等待产品出来,有产品就使用它。这里我们使用一个变量来表示这个这个产品,生产者生产一件产品变量加 1,消费者消费一次变量减 1,

Dola的博客 1090

线程中信号量与条件变量详解

线程中信号量与条件变量详解(篇幅过长,请谨慎查阅)

m0_73932536的博客 927

Java线程详解(16)-条件变量

        条件变量是Java5线程中很重要的一个概念,顾名思义,条件变量就是表示条件的一种变量。但是必须说明,这里的条件是没有实际含义的,仅仅是个标记而已,并且条件的含义往往通过代码来赋予其含义。         这里的条件和普通意义上的条件表达式有着天壤之别。         条件变量都实现了java.util.concurrent.locks.Condition接口,条件变量的实例化...

勇往直前的专栏 408

详解Linux多线程中锁、条件变量、信号量

今天我们继续来聊一聊Linux中多线程编程中的重要知识点,详细谈谈多线程中同步和互斥机制。

公众呺:Linux兵工厂, 加关+私信获取海量Linux干货资料和代码 721

linux线程中互斥锁和条件变量的使用(详解)

互斥锁pthread_mutex_t 在linux中,互斥锁的出现是为了限制多个线程同时对临界资源区进行访问。通过互斥锁对临界资源区进行保护,只有拥有锁的线程才可以访问临界区,没有的锁的线程如果要访问临界区则需要等到锁的释放后,竞争到锁的拥有权后,才能进入临界区。 但这里会出现一个问题:单纯加锁也会导致一直是一个线程访问临界资源的问题,个别线程竞争力很强,可能会一直占据锁的使用权,导致其他线程的无法进入临界区。    这就需要引入另一个机制 条件变量 条件变量pthread_cond_t 条件变量被用于线程

KUNPLAYBOY的博客 2095

C++多线程详解 | 线程创建 | 互斥锁 | 条件变量 | 线程池

C++多线程详解 | 线程创建 | 互斥锁 | 条件变量 | 线程池

weixin_45754224的博客 2352

通用线程:POSIX 线程详解,第 3 部分 使用条件变量提高效率

通用线程:POSIX 线程详解,第 3 部分 使用条件变量提高效率

tattarrattat的专栏 837
上一篇: openssl 客户端程序移植到arm平台 OK6410上运行
下一篇: dlsym与-fPIC,以及objcopy
RICH_BABA
博客等级 码龄17年 51粉丝 17原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值