当前位置:网站首页>std::condition_ variable::wait_ for
std::condition_ variable::wait_ for
2022-06-13 05:02:00 【Snow * sleet * snow】
wait:https://blog.csdn.net/qq_34999565/article/details/120874408?utm_source=app&app_version=4.17.0&code=app_1562916241&uLinkId=usr1mkqgl919blen
Examples of use , and wait Is the difference between the wait_for You can set a timeout
#include <iostream>
#include <atomic>
#include <condition_variable>
#include <thread>
#include <chrono>
using namespace std::chrono_literals;
std::condition_variable cv;
std::mutex cv_m;
int i;
void waits(int idx)
{
std::unique_lock<std::mutex> lk(cv_m);
if(cv.wait_for(lk, idx*100ms, []{
return i == 1;}))
std::cerr << "Thread " << idx << " finished waiting. i == " << i << '\n';
else
std::cerr << "Thread " << idx << " timed out. i == " << i << '\n';
}
void signals()
{
std::this_thread::sleep_for(120ms);
std::cerr << "Notifying...\n";
cv.notify_all();
std::this_thread::sleep_for(100ms);
{
std::lock_guard<std::mutex> lk(cv_m);
i = 1;
}
std::cerr << "Notifying again...\n";
cv.notify_all();
}
int main()
{
std::thread t1(waits, 1), t2(waits, 2), t3(waits, 3), t4(signals);
t1.join();
t2.join();
t3.join();
t4.join();
}
Output :
Thread 1 timed out. i == 0
Notifying…
Thread 2 timed out. i == 0
Notifying again…
Thread 3 finished waiting. i == 1
2
3
边栏推荐
- 2021TPAMI/图像处理:Exploiting Deep Generative Prior for Versatile Image Restoration and Manipulation
- Chapter 18 pagination: Introduction
- Explain the differences and usage scenarios between created and mounted
- Kaggle 时间序列教程
- Section 2 - branch and loop statements
- C language learning log 10.10
- Sampo Lock
- Recursion and recursion
- Win8.1和Win10各自的優勢
- C language learning log 1.17
猜你喜欢
Spice story
Explain the role of key attribute in V-for
Optocoupler working principle function electric parameter application circuit
Shell built-in string substitution
自动评教脚本使用的配置
Mysql8.0.13 installation tutorial (with pictures)
Interpretation of QT keypressevent
QT signal is automatically associated with the slot
Understanding inode
Analysis of scoped attribute principle and depth action selector
随机推荐
On switch() case statement in C language
QT using layout manager is invalid or abnormal
Draw a hammer
Chapter 13 abstraction: address space
metaRTC4.0集成ffmpeg编译
C language learning log 2.6
Regular expressions in QT
Promise processing JS multithreads get the same processing result after all the results are obtained
Simple sr: Best Buddy Gans for highly detailed image super resolution Paper Analysis
Analysis on the usage, response and global delivery of provide/inject
Advanced C - Section 3 - character functions and string functions
Advantages of win8.1 and win10
Logical point
C language learning log 10.8
RuoYi-Cloud启动教程(手把手图文)
lookup
Elliptic curve encryption
Analysis of the principle of V-model and its application in user defined components
Dup2 use
Clause 31: avoid default capture mode