question about STL thread-safe and STL debugging

前端 未结 4 1842
不思量自难忘°
不思量自难忘° 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条回答
  •  不要未来只要你来
    2021-01-06 11:19

    The standard c++ containers are not thread safe because you most likely actually want higher level locking than just the containers themselves. In other words you are likely to want two or more operations to be safe together.

    For example, if you have multiple threads running:

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

    You wont get a nice vector of alternating 0's and 1's, they could be jumbled. You would need to lock around both commands to get what you want.

提交回复
热议问题