I'm not sure the question is easily understandable, so I'll start with a (faulty) code of what I would like to do.
...
{
Time start = getCurrentTime();
scoped_lock<MutexType> lock(mutex);
Time lockWait = getCurrentTime() - start;
// Protected section
...
}
This code does not work because nothing guarantees that the thread will not be preempted somewhere between the start time save instruction and the mutex lock, in which case the computed lockWait would be wrong (overestimation).
I can not think of any solution to get around this issue. I feel that I need to get my hands dirty in multithread low level mechanisms, but I do not know how. Would you have any solution or pointer?
So I'm interested in the wait time, obviously, but I also would like to know if the lock is contested or not, which I'm not sure I can obtain with time measurements only. If not, how could I get infos about that?
PS. I've seen there exists some tools that provide some measurements, such as mutrace or even Walgrind, but unfortunately it is required for my measurements to be integrated to the application source code.