Is there a quick way to log that a thread is waiting for a lock when a thread arrives at a
lock(x)
{
//do work on a collection
}
?
If thread 1 arrives at the lock, and there isn’t another thread in the block, there should be no log. Then if thread 2 arrives at the lock(x) and thread 1 is still in the block, there should be a log. The ideal solution would not require switching to a Mutex or Monitor add much complexity. If there’s a quick and easy way to do this using TPL, PLINQ, or RX that would be great.
You can de-sugar the
lockstatement and use Monitor.TryEnter to check if the lock can be acquired without waiting: