Let’s say we have MethodA and MethodB where whole methods’ bodies are within single big lock block. Both methods use the same object for locking:
lock (objectX)
{
// methodbody
}
There are multiple threads that call these two methods.
Two questions:
- Is it possible to measure total wait time? Including: time to obtain lock and time required by “other” method’s lock block to finish execution.
- Would it be faster to introduce locks in both methods on appropriate places instead of encapsulating whole method body with single lock? I realize that here i need to measure performance of both solutions, but I would like to hear your experience and recommendations
Yes. Always keep locked segments as short as possible. The locking itself is cheap, dont keep others waiting while you do non-critical things.
Not this precisely I think. For long stretches you might look into CPU time vs Elapsed time but in general: use a good profiler.