Is using std::deque or std::priority_queue thread-safe?

grich picture grich · Nov 5, 2010 · Viewed 17.4k times · Source

Possible Duplicates:
Is the C++ STL std::set thread-safe?
Thread safety for STL queue

I'm guessing it isn't, I just want to make sure. meaning 2 threads using the same std::deque using std::deque::push_back or push_front at the same time.

Same question goes for std::priority_queue and the functions std::priority_queue::push and std::priority_queue::pop..

Are those containers thread-safe? Or I should personally program it to be thread-safe?

Tnx a lot.

Answer

DumbCoder picture DumbCoder · Nov 5, 2010

From Scott Myer's Effective STL Item 12. Have realistic expectations about the thread safety of STL containers

Multiple readers are safe. Multiple threads may simultaneously read the contents of a single container, and this will work correctly. Naturally, there must not be any writers acting on the container during the reads.

Multiple writers to different containers are safe. Multiple threads may simultaneously write to different containers.

When it comes to thread safely and STL containers, you can hope for a library implementation that allows multiple readers on one container and multiple writers on separate containers. You can't hope for the library to eliminate the need for manual concurrency control, and you can't rely on any thread support at all.