如何分析 C++基于STL的程序来检测STL互斥锁?

发布于 2024-10-08 23:10:09 字数 282 浏览 0 评论 0原文

受到 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

So要识趣 2024-10-15 23:10:09

使用写入时复制的 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.

初懵 2024-10-15 23:10:09

通常,您无法在堆栈回溯中检测互斥锁的使用情况。当然也有例外(堆栈上的 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."

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文