C ++ thread safety std Containers

I read a lot of posts here asking if the standard containers for C ++ (for example, "list" or "map") are thread safe, and they all said that this is not at all. Parallel reads should be fine, but parallel writes or parallel reads and writes can cause problems.

Now I learned that at www.cplusplus.com access to or change the list during most operations is safe.

Some examples:

map :: find

Access to the container (neither constant nor non-contact versions change the container). Access to displayed values ​​is not available: simultaneous access or change of elements is safe.

map :: insert

The container is modified. Simultaneous access to existing elements is safe, although there are no iterative ranges in the container.

Could I understand what I know about thread safety in std containers.

Thanks in advance!

PS: I ask for C ++ 03, not for C ++ 11

+7
c ++ multithreading stl c ++ 03
source share
4 answers

Sounds right.

Please note that access to map values ​​from multiple threads, if you change the actual value, will also need to be protected. If you KNOW that two threads update DIFFERENT records (I don't mean insert / delete), then this is safe.

+5
source share

Parallel reads should be fine, but parallel writes or parallel reads and writes can cause problems.

It is right. This is a guarantee generally offered for unsynchronized access to objects in C ++. Such "problems" are formally called data races.

Now I learned that at www.cplusplus.com access to or change of the list during most of the operations is safe.

No, containers do not offer more than a basic guarantee for concurrent readings. There will be a data race if one thread accesses it and the other modifies it. However, in some containers it is sometimes safe to access the elements of the container while the container itself is modified.

The first example says that find does not change the value of the container or access element (keys only), therefore it is safe if it is accessed by other threads, or by changing (different) values ​​without changing the container itself.

The second example means that you can safely access an existing element (using a link or an iterator to this element), since inserting an element will not interfere with the existing one.

I ask for C ++, not for C ++ 11

Nowadays, C ++ is C ++ 11. If you ask about historical versions of the language, they had nothing to say about threads, so the question does not answer at all, only for the specific implementation and structure of the threads.

+11
source share

Prior to C ++ 11, there was no concept of a stream in the standard. Thus, the question of whether a container is thread safe does not make sense in the context of C ++ 03.

+3
source share

As Marsin noted, C ++ 03 has no concept of flow; therefore, you cannot assume any thread-safe operation even for parallel reading in two streams after a full write.

Think about this case: At t = 0 you create a stream, let A At t = 10 sec, stream B (which exists before creating stream A) is written to the container. At t = 1 hour, threads A and B try to read the container without any synchronization through a third-party library (for example, pthread).

C ++ 03 ensures that thread B sees the correct value. But there is no guarantee that thread A will see the correct value, because C ++ 03 expects each program to be a single thread, so the C ++ 03 specification can only guarantee a sequence of visible events in a programmed order (as if in 1 thread) .

0
source share

All Articles