Concurrency 4, condition_variable - C++11, 11 of n
来源:互联网 发布:淘宝关键词工具 编辑:程序博客网 时间:2024/06/06 08:40
1) condition_variable
#include <condition_variable>
#include <mutex>
#include <future>
#include <thread>
#include <iostream>
#include <queue>
std::queue<int> queue;
std::mutex queueMutex;
std::condition_variable queueCondVar;
void provider (int val)
{
for (int i=0; i<6; ++i) {
{
std::lock_guard<std::mutex> lg(queueMutex);
queue.push(val+i);
} // release lock
queueCondVar.notify_one(); // Note, without holding the mutex, which is good for C++11
std::this_thread::sleep_for(std::chrono::milliseconds(val));
}
}
void consumer (int num)
{
while (true) {
int val;
{
std::unique_lock<std::mutex> ul(queueMutex);
queueCondVar.wait(ul,[]{ return !queue.empty(); });
val = queue.front();
queue.pop();
} // release lock
std::cout << "consumer " << num << ": " << val << std::endl;
}
}
int main()
{
auto p1 = std::async(std::launch::async,provider,100);
auto p2 = std::async(std::launch::async,provider,300);
auto p3 = std::async(std::launch::async,provider,500);
auto c1 = std::async(std::launch::async,consumer,1);
auto c2 = std::async(std::launch::async,consumer,2);
Called without the predicate, both wait_for() and wait_until() return the following enumeration class values:
- std::cv_status::timeout if the absolute timeout happened
- std::cv_status::no_timeout if a notification happened
Called with a predicate as third argument, wait_for() and wait_until() return the result of the predicate (whether the condition holds).
2) Note:
- Notifications are automatically synchronized so that concurrent calls of notify_one() and notify_all() cause no trouble.
- All threads waiting for a condition variable have to use the same mutex, which has to be locked by a unique_lock when one of the wait() members is called. Otherwise, undefined behavior occurs.
- Note that consumers of a condition variable always operate on mutexes that are usually locked. Only the waiting functions temporarily unlock the mutex performing the following three atomic steps (The problem with a naive approach like “lock, check state, unlock, wait” is that notifications arising between unlock and wait would get lost.):
3) Example
- Unlocking the mutex and entering the waiting state
- Unblocking the wait
- Locking the mutex again
#include <condition_variable>
#include <mutex>
#include <future>
#include <thread>
#include <iostream>
#include <queue>
std::queue<int> queue;
std::mutex queueMutex;
std::condition_variable queueCondVar;
void provider (int val)
{
for (int i=0; i<6; ++i) {
{
std::lock_guard<std::mutex> lg(queueMutex);
queue.push(val+i);
} // release lock
queueCondVar.notify_one(); // Note, without holding the mutex, which is good for C++11
std::this_thread::sleep_for(std::chrono::milliseconds(val));
}
}
void consumer (int num)
{
while (true) {
int val;
{
std::unique_lock<std::mutex> ul(queueMutex);
queueCondVar.wait(ul,[]{ return !queue.empty(); });
val = queue.front();
queue.pop();
} // release lock
std::cout << "consumer " << num << ": " << val << std::endl;
}
}
int main()
{
auto p1 = std::async(std::launch::async,provider,100);
auto p2 = std::async(std::launch::async,provider,300);
auto p3 = std::async(std::launch::async,provider,500);
auto c1 = std::async(std::launch::async,consumer,1);
auto c2 = std::async(std::launch::async,consumer,2);
}
Footnote:
std::unique_lock<std::mutex> ul(queueMutex);
queueCondVar.wait(ul,[]{ return !queue.empty(); });
"Equals"
std::unique_lock<std::mutex> ul(queueMutex);
while(queue.empty()) {
queueCondVar.wait(ul);
}
4) condition_variable_any
It does not require using an object of class std::unique_lock as lock, so you can provide your own lock implementation but the class should implement lock and unlock member function
- Concurrency 4, condition_variable - C++11, 11 of n
- Concurrency 1, async and future - C++11, 8 of n
- Concurrency 2, thread and promise - C++11, 9 of n
- Concurrency 3, mutex and lock - C++11, 10 of n
- Concurrency 5, atomics - C++11, 12 of n
- C++11 std::condition_variable
- mutex and condition_variable[c++11]
- 《C++ Concurrency in Action》笔记14 condition_variable
- C++11 多线程编程《C++ Concurrency in Action》读书笔记(1)-Hello World of Concurrency in C++
- 1 Hello, world of concurrency in C++!
- c++11之condition_variable学习笔记
- c++11中的thread, mutex, condition_variable讲解
- c++ 11 多线线程系列----condition_variable
- c++11条件变量的使用,condition_variable
- C++11多线程(八):std::condition_variable 详解
- C++11 并发指南std::condition_variable详解
- c++11多线程编程之condition_variable
- C++11中std::condition_variable的使用
- srand 函数
- 字符串去除特定字符
- MATLAB "Out of memory"问题
- 异步下载图片和图片缓存
- 在路上
- Concurrency 4, condition_variable - C++11, 11 of n
- Hadoop相关启动脚本分析
- hdu 4411 Arrest(费用流)
- RAND_MAX 宏
- Hadoop学习笔记(一)HBase脚本分析(二)hbase-daemon.sh
- NetBeans下配置glassfish服务器
- ADS与IAR的相互移植
- Oracle密码忘记解决方法
- Hadoop的shell脚本分析