kagali-san
kagali-san

Reputation: 3082

How to profile C++ STL-based program to detect STL mutex locks?

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?

Upvotes: 0

Views: 444

Answers (2)

Öö Tiib
Öö Tiib

Reputation: 11011

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.

Upvotes: 2

Bukes
Bukes

Reputation: 3718

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."

Upvotes: 1

Related Questions