Jump to content
  • Advertisement
Sign in to follow this  
littlekid

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

This topic is 3161 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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
Advertisement
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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!