Bhush
Bhush

Reputation: 66

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. 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

Answers (2)

sjdowling
sjdowling

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

Jiří Pospíšil
Jiří Pospíšil

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

Related Questions