如何分析 C++基于STL的程序来检测STL互斥锁?
受到 std::string in a multi-threaded-program 和另一个的启发答案(在某处看到),STL (gcc) std::string 可以通过在读取/设置值时设置一种互斥锁来阻止多线程程序。 (我的代码中没有互斥体)。
蜂巢头脑,请回答我:这是真的吗?如果可能的话,如何检测 gdb 回溯中互斥锁的使用情况?
Inspired by std::string in a multi-threaded program and another answer (seen somewhere), that STL (gcc) std::string can block the multithreaded program by setting a kind of mutex when reading/setting the value. (no mutexes in my code).
Hive mind, please answer me: is this true? how, if possible, do I detect mutex usage in gdb's backtrace?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用写入时复制的 std::string 实现需要在复制的线程之间进行同步,但不需要互斥体。非阻塞同步在大多数平台上就足够了,因为大多数平台可以组织对引用计数的原子访问。
查看 std::basic_string<> 的方式很简单。实现了,并不是那么大的一段代码。我见过的大多数实现都使用短字符串优化,而不是写入时复制。
The std::string implementation that uses copy on write needs to synchronize between threads that copying but mutexes are not needed for that. Non-blocking synchronization is sufficient on most platforms since most platforms can organize atomic access to reference count.
It is simple to look at how your std::basic_string<> is implemented, it is not so large piece of code. Most implementations i have seen use short string optimization and not copy on write.
通常,您无法在堆栈回溯中检测互斥锁的使用情况。当然也有例外(堆栈上的 RAII 锁类型对象)。
确定您正在使用的标准 C++ 库实现正在执行的操作的最佳方法是检查源代码、使用调试器单步调试它,或两者兼而有之。
正如几位智者过去所说:“了解你的工具。”
Generally, you won't be able to detect mutex usage in a stack back trace. There are of course exceptions to this (RAII lock type objects on the stack).
The best way to determine what the Standard C++ library implementation that you're using is doing is to examine the source, step through it with your debugger, or both.
As several wise men have said in the past: "Know your tools."