Question about STL thread safety and STL debugging

I have two questions about STL

1) why is STL not thread safe? Is there any structure that is thread safe?

2) How to debug STL using GDB? In GDB, how can I print a vector?

+3
source share
4 answers
  • Container data structures almost always require synchronization (e.g. mutex ) to prevent race conditions . Since threads are not supported by the C ++ standard (pre C ++ 0x), they cannot be added to the STL. In addition, synchronization is very expensive in cases where it is not needed. STL containers can be used in multi-threaded applications while you perform this synchronization manually. Alternatively, you can create your own thread-safe containers that are compatible with STL algorithms such as this streaming ring queue .
  • A vector . , , , . STL.
+3

++ , , , , . , , , , .

, :

v.push_back(0);
v.push_back(1);

0 1, . , , .

+3

STL is not thread safe because many people do not need thread safety, and because it introduces the context of streaming into classes that would otherwise not need to know anything about the concept of threads.

You can encapsulate access to containers and provide your own thread safety (or other restrictions imposed by your specific design and implementation).

+1
source
  • Because there are still single-threaded programs.
  • Take a look here .
+1
source

Source: https://habr.com/ru/post/1740933/


All Articles