关于STL线程安全和STL调试的问题
我有两个关于STL的问题
1)为什么STL不是线程安全的?有没有线程安全的结构?
2)如何使用GDB调试STL?在GDB中,如何打印矢量?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
向量
包含一个连续的内存块。因此,一旦找到指向该内存块的指针,它就可以像常规数组一样显示。确切的细节取决于您使用的 STL 实现。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.标准 C++ 容器不是线程安全的,因为您很可能实际上需要比容器本身更高级别的锁定。换句话说,您可能希望两个或多个操作一起安全进行。
例如,如果您有多个线程正在运行:
您不会获得交替的 0 和 1 的良好向量,它们可能会混乱。您需要锁定这两个命令才能获得您想要的结果。
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:
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.
STL 不是线程安全的,因为很多人不需要线程安全,并且因为这将线程上下文引入到类中,否则这些类不需要了解任何有关线程概念的信息。
您可以封装对容器的访问并提供您自己的线程安全性(或您的特定设计和实现所施加的其他限制。)
STL is not thread-safe because a lot of people don't need thread safety, and because that introduces threading context into classes that otherwise have no 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.)