当前位置:网站首页>并发-条件变量
并发-条件变量
2022-06-21 09:13:00 【彭同学她同桌】
std::condition_variable
本质是一个类
一般作用于:
线程A:等待一个条件达成时再运行
函数
std::condition_variable mcv//创建一个条件变量
std::mutex mtex;//创建一个互斥量
vector<int> v;
wait()
如果第二个参数是false 那么wait()将解锁互斥量 并堵塞本行 堵塞到其他某个线程调用notify_one()为止 当在其他线程B中调用完notify_one()后 wait()的线程A就会被唤醒 此时A线程并不会立刻运行后面的代码 而是不断的尝试去获取互斥量锁 如果锁住了 会再次判断wait()的第二个参数
因为在wait()之前会先锁住互斥量 如果wait()第二个参数返回的是false 还不对互斥量解锁的话 则会卡死
如果不写第二个参数 默认是false 直接堵塞
std::unique_lock<std::mutex>mUniqueTex(mtex);
mcv.wait(mUniqueTex,[]{
if(!v.empty())
return true;
return false;
})
notify_one()
尝试唤醒一个wait()的线程
mcv.notify_one();
notify_all()
尝试唤醒所有wait()的线程
mcv.notify_all();
边栏推荐
- Binary search (integer binary)
- 【实战】STM32MP157开发教程之FreeRTOS系统篇3:FreeRTOS 计数型信号量
- finally block can not complete normally
- Dumpling备份数据库
- Six methods of optimizing inventory management in food production industry
- Retrofit Extended reading
- Style penetration of vant UI components -- that is, some styles in vant UI components cannot be modified
- leetcode:19. Delete the penultimate node of the linked list
- 《网络是怎么样连接的》读书笔记 - FTTH
- Merge sort of sorting
猜你喜欢

stm32mp1 Cortex M4开发篇13:扩展板按键外部中断

\Processing method of ufeff

【实战】STM32MP157开发教程之FreeRTOS系统篇6:FreeRTOS 列表和列表项

Tidb3.0- 4.0 memory control / modification log saving days / maximum index length

Mono fourni avec l'unit é 5 peut également supporter C # 6

Client construction and Optimization Practice

How to connect the Internet - FTTH

Abstractqueuedsynchronizer (AQS) source code detailed analysis - condition queue process analysis

A command starts the monitoring journey!

《网络是怎么样连接的》读书笔记 - FTTH
随机推荐
【活动早知道】LiveVideoStack近期活动一览
Tidb3.0- 4.0 memory control / modification log saving days / maximum index length
R language uses strptime function, format to format output parameters, and R environment options parameters to convert strings into time objects (the output time information includes time, minutes, se
【实战】STM32MP157开发教程之FreeRTOS系统篇3:FreeRTOS 计数型信号量
STL教程3-类型转换static_cast、dynamic_cast、const_cast、reinterpret_cast方法
【实战】STM32 FreeRTOS移植系列教程4:FreeRTOS 软件定时器
如何监听DOM元素尺寸的变化?
Qsort sort string
R language through rprofile Site file, user-defined configuration of R language development environment startup parameters, shutdown parameters, user-defined specified cran local image source download
Thread pool source code analysis_ 01 futuretask source code analysis
finally block can not complete normally
STL tutorial 2-myarray framework implementation
Three key directories in R language and their corresponding priorities: R_ Home directory, user directory, current working directory, files read by R's startup process
Dumpling备份数据库
C # implement callback
客户端建设及调优实践
Doc common syntax, updating
apk 反编译 上的填坑之路
【C】【时间操作】C语言中关于时间的操作
Unity . Net framework