admin管理员组文章数量:1599540
https://en.cppreference/w/cpp/thread/condition_variable
头文件
<condition_variable>
声明
class condition_variable;(since C++11)
描述
condition_variable类是一个同步原语,可用于阻塞一个线程或同时阻塞多个线程,直到另一个线程修改共享变量(条件)并通知condition_variable。
尝试修改共享变量的线程必须:
- 获取一个std :: mutex(通常通过std :: lock_guard).
- 持有该锁的线程执行修改.
- 在std :: condition_variable上执行notify_one或notify_all(该锁不需要保留以进行通知).
即使共享变量是原子变量,也必须在互斥量的保护下对其进行修改,以便正确地将修改发布到等待的线程。
任何在std :: condition_variable上等待的线程都必须
- 获取std :: unique_lock <std :: mutex>,用同一个互斥锁来保护共享变量.
- 或者
- 执行wait,wait_for或wait_until。等待操作以原子方式释放互斥锁并挂起当前线程的执行。
- 条件变量被通知,超时到期或发生虚假唤醒时,将唤醒线程,并以原子方式重新获取互斥量。然后,线程检查条件变量,如果唤醒是虚假的,则应继续等待。
std :: condition_variable只能与std :: unique_lock <std :: mutex>配合使用;此限制允许在某些平台上获得最大效率。 std :: condition_variable_any提供了可与任何BasicLockable对象一起使用的条件变量,例如std :: shared_lock。
条件变量通过notify_one和notify_all成员函数允许并发唤醒wait、wait_for、wait_until。
std::condition_variable 类是StandardLayoutType类型, 它不能 CopyConstructible, MoveConstructible, CopyAssignable, or MoveAssignable.
成员
Member types
Member type | Definition |
native_handle_type | implementation-defined |
Member functions
(constructor) | constructs the object (public member function) |
(destructor) | destructs the object (public member function) |
operator= [deleted] | not copy-assignable (public member function) |
Notification | |
notify_one | notifies one waiting thread (public member function) |
notify_all | notifies all waiting threads (public member function) |
Waiting | |
wait | blocks the current thread until the condition variable is woken up (public member function) |
wait_for | blocks the current thread until the condition variable is woken up or after the specified timeout duration (public member function) |
wait_until | blocks the current thread until the condition variable is woken up or until specified time point has been reached (public member function) |
Native handle | |
native_handle | returns the native handle (public member function) |
举例
condition_variable
跟 std::mutex结合使用,这样就能实现线程间的交互。
#include <iostream> #include <string> #include <thread> #include <mutex> #include <condition_variable> std::mutex m; std::condition_variable cv; std::string data; bool ready = false; bool processed = false; void worker_thread() { // Wait until main() sends data std::unique_lock<std::mutex> lk(m); cv.wait(lk, []{return ready;}); // after the wait, we own the lock. std::cout << "Worker thread is processing data\n"; data += " after processing"; // Send data back to main() processed = true; std::cout << "Worker thread signals data processing completed\n"; // Manual unlocking is done before notifying, to avoid waking up // the waiting thread only to block again (see notify_one for details) lk.unlock(); cv.notify_one(); } int main() { std::thread worker(worker_thread); data = "Example data"; // send data to the worker thread { std::lock_guard<std::mutex> lk(m); ready = true; std::cout << "main() signals data ready for processing\n"; } cv.notify_one(); // wait for the worker { std::unique_lock<std::mutex> lk(m); cv.wait(lk, []{return processed;}); } std::cout << "Back in main(), data = " << data << '\n'; worker.join(); }
输出:
main() signals data ready for processing Worker thread is processing data Worker thread signals data processing completed Back in main(), data = Example data after processing
本文标签: 变量条件conditionvariablestd
版权声明:本文标题:条件变量std::condition_variable 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://www.elefans.com/dongtai/1728322700a1154033.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论