Reputation: 66
I have a typical scenario: Let's say thread A and thread B are going to access std:list. Thread A is going to push an item to list and thread B is going to pop the item from the list.
My question is: How to make thread B wait until there is a data inside the queue.
Upvotes: 0
Views: 1478
Reputation: 3022
Instead of using std::list
you probably want to use a container with thread safety built in such as boost::lockfree::queue
or TBB concurrent_queue
.
Upvotes: 3
Reputation: 14412
How to make thread B wait until there is a data inside the queue.
Check out 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.
Upvotes: 2