简体   繁体   English

如何使线程在std :: list上等待值?

[英]How to make a thread wait on std::list for a value?

I have a typical scenario: Let's say thread A and thread B are going to access std:list. 我有一个典型的场景:假设线程A和线程B将访问std:list。 Thread A is going to push an item to list and thread B is going to pop the item from the list. 线程A会将项目推入列表,线程B将项目从列表中弹出。

My question is: How to make thread B wait until there is a data inside the queue. 我的问题是:如何使线程B等待队列中没有数据。

而不是使用std::list ,你可能希望使用内置的诸如线程安全的容器boost::lockfree::queueTBB concurrent_queue

How to make thread B wait until there is a data inside the queue. 如何使线程B等待直到队列中有数据。

Check out std::condition_variable . 检出std :: condition_variable With a condition variable, a thread can wait (spin and then sleep) on the variable until a different thread signals on the same variable and wakes up the waiting thread. 使用条件变量,线程可以在变量上等待(旋转然后进入睡眠状态),直到另一个线程在同一变量上发出信号并唤醒等待的线程为止。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM