当前位置:网站首页>Concurrency - condition variable
Concurrency - condition variable
2022-06-21 09:16:00 【Classmate Peng is her deskmate】
std::condition_variable
The essence is a class
Generally, it works on :
Threads A: Wait for a condition to be met before running
function
std::condition_variable mcv// Create a condition variable
std::mutex mtex;// Create a mutex
vector<int> v;
wait()
If the second parameter is false that wait() Will unlock mutex And block the bank Blocking some other thread to call notify_one() until When in other threads B End of call notify_one() after wait() The thread of A Will be awakened here A Threads do not immediately run the following code Instead, you keep trying to get the mutex lock If it's locked Will judge again wait() Second parameter of
Because in wait() The mutex will be locked before If wait() The second parameter returns false If the mutex is not unlocked You'll get stuck
If you don't write the second parameter The default is false Direct blockage
std::unique_lock<std::mutex>mUniqueTex(mtex);
mcv.wait(mUniqueTex,[]{
if(!v.empty())
return true;
return false;
})
notify_one()
Try to wake up a wait() The thread of
mcv.notify_one();
notify_all()
Try to wake up all wait() The thread of
mcv.notify_all();
边栏推荐
- 【MGT】代码解读之model-MGT
- 【实战】STM32MP157开发教程之FreeRTOS系统篇6:FreeRTOS 列表和列表项
- [actual combat] STM32 FreeRTOS migration series tutorial 2: FreeRTOS mutually exclusive semaphores
- Doc common syntax, updating
- JUnit5单元测试
- 2. the development of the meta universe
- Source insight shortcut key cross reference
- Retrofit extended reading
- PingCAP 入选 2022 Gartner 云数据库“客户之声”,获评“卓越表现者”最高分
- Character function and string function
猜你喜欢

Stm32mp1 cortex M4 development part 13: external interrupt of expansion board key

Lei niukesi --- basis of embedded AI
![[practice] STM32 FreeRTOS porting series tutorial 1: use of FreeRTOS binary semaphores](/img/47/611640b817d3e1573c2cde25a9f2e5.jpg)
[practice] STM32 FreeRTOS porting series tutorial 1: use of FreeRTOS binary semaphores

The spring recruitment is also terrible. Ali asked at the beginning of the interview: how to design a high concurrency system? I just split

【MGT】代码解读之model-MGT

Windows10 LAN shared folder process

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

STL教程3-类型转换static_cast、dynamic_cast、const_cast、reinterpret_cast方法

Request and response must know

Abstractqueuedsynchronizer (AQS) source code detailed analysis - countdownlatch source code analysis
随机推荐
Summary of Web automated testing
Doc common syntax, updating
一条命令开启监控之旅!
Idea common code templates
Ali has been working for 8 years. This learning note is left when he reaches P8. He has helped his friends get 10 offers
stm32mp1 Cortex M4开发篇9:扩展板空气温湿度传感器控制
1. is god horse a meta universe?
Jar package required for full stack development
[actual combat] STM32 FreeRTOS porting series Tutorial 4: FreeRTOS software timer
[actual combat] STM32 FreeRTOS migration series tutorial 2: FreeRTOS mutually exclusive semaphores
Audio immersive experience
Abstractqueuedsynchronizer (AQS) source code analysis - cyclicbarrier source code analysis
Wechat applet
[vs], [usage problem], [solution] when VS2010 is opened, it stays in the startup interface
Adapt to the pits in Huawei models
doc常用语法,更新中……
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
Retrofit Extended reading
Quick sort_ sort
Thread pool source code analysis_ 01 futuretask source code analysis