Do I need to block the STL list with the mutex in the push_back pop_front script?

I have a push-backing stream to the STL list and another stream pop-fronting from the list. Do I need to block the list with the mutex in this case?

+6
c ++ multithreading stl
source share
5 answers

From SGI STL to protect streams :

If several threads access the same container and at least one thread can potentially write, then the user is responsible for ensuring mutual exclusion between the threads during access to the container.

Since your threads are modifying the list, I think you need to block it.

+11
source share

Most STL implementations are thread safe, as you can access multiple instances of a list type from multiple threads without blocking. But you MUST block when you access the same instance of your list.

Look at this for more info: safty stream in sgi stl

+3
source share

Maybe. These operations are not simple enough to be atomic, so they will only be thread safe if the implementation explicitly performs the necessary lock.

However, the C ++ standard does not indicate whether these operations should be thread safe, so solving this problem depends on the particular implementation. Check documents. (Or tell us which implementation you are using)

+1
source share

There is no guarantee that the STL implementation is thread safe, and since it costs performance, I would suggest that most of them are not. You must definitely use a mutex.

+1
source share

Since stl pop / push AFAIK operations are non-atomic, you need to use a mutex.

+1
source share

All Articles