Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

question about STL thread-safe and STL debugging

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 print a vector?

like image 707
skydoor Avatar asked Jan 23 '23 06:01

skydoor


1 Answers

  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.
like image 95
Judge Maygarden Avatar answered Jan 24 '23 21:01

Judge Maygarden