question about STL thread-safe and STL debugging

前端 未结 4 1845
不思量自难忘°
不思量自难忘° 2021-01-06 10:33

I have two questions about STL

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

2) How to debug STL using GDB? In GDB, how can I p

4条回答
  •  梦毁少年i
    2021-01-06 11:11

    1. Container data structures almost always require synchronization (e.g. a mutex) to prevent race conditions. Since threading is not support by the C++ standard (pre C++0x), these could not be added to the STL. Also, synchronization is very expensive for cases where it is not needed. STL containers may be used in multi-threaded applications as long as you perform this synchronization manually. Alternatively, you may create your own thread-safe containers that are compatible with STL algorithms like this thread-safe circular queue.
    2. A vector contains a contiguous block of memory. So, it can be displayed in the same way as a regular array once you find the pointer to this memory block. The exact details depend on the STL implementation you use.

提交回复
热议问题