Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / programming / threads

Locking multiple (up to 2) objects

0.00/5 (No votes)
25 Dec 2011CPOL 26K  
This code is buggy.See the line:if (Monitor.TryEnter(po1) && Monitor.TryEnter(po2)) return;Imagine that the lock to po1 is true, but for po2 is false (or vice-versa).It will not return, and later will lock po1 again.So, when unlocking, it will unlock the wrong number of...
This code is buggy.
See the line:
C#
if (Monitor.TryEnter(po1) && Monitor.TryEnter(po2))
  return;


Imagine that the lock to po1 is true, but for po2 is false (or vice-versa).
It will not return, and later will lock po1 again.
So, when unlocking, it will unlock the wrong number of times.

I really think that if you always lock in the same order with conventional lock:
C#
lock(po1)
{
  lock(po2)
  {
    // code.
  }
}


or if you get rid of one of the locks, you will have better code.
If that's not the case, I think a better implementation will be like this:
Receive an array of objects to lock (so, it can have more than two).
It will create an array of bools of the same size.
At each try, it will only try to lock if lock was not already taken for the given index (using that boolean array).
When locking, it will use TryEnter(1, ref locksTaken[lockIndex]);

And, if an exception is thrown, it will unlock all already taken locks.
But to be honest, even if this approach never deadlocks, it will timeout to often by "dead-lock" conditions... it is better to enforce lock ordering.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)