Sign in to follow this  
littlekid

[C++] std::list and thread safety

Recommended Posts

hihi, I would like to know is it safe to do operation on a std::list from different thread?? for e.g I have 2 list
std::list<int> listA, listB;

Thread 1: iterate through listA and print numbers;
Thread 2: splice one/many elements of list A to list B
Personally I think its not thread safe, but just decided to ask to confirm my suspicions. If its really not thread safe, is there anyway to avoid locking before splicing each list elements?? currently I am thinking about Concurrency::event over locks, but I would like to avoid that too.

Share this post


Link to post
Share on other sites
None of the standard containers are thread safe. You need to surround them with locks (of any variety) if they're going to be used by multiple threads.

On Windows, you could consider using an Interlocked Singly Linked List, which is completely thread safe and has better performance than lock-based synchronisation.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this