Click here to Skip to main content
15,867,870 members
Articles / General Programming / Threads
Alternative
Tip/Trick

Locking multiple (up to 2) objects

Rate me:
Please Sign up or sign in to vote.
0.00/5 (No votes)
25 Dec 2011CPOL 23.2K   10
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)


Written By
Software Developer (Senior) Microsoft
United States United States
I started to program computers when I was 11 years old, as a hobbyist, programming in AMOS Basic and Blitz Basic for Amiga.
At 12 I had my first try with assembler, but it was too difficult at the time. Then, in the same year, I learned C and, after learning C, I was finally able to learn assembler (for Motorola 680x0).
Not sure, but probably between 12 and 13, I started to learn C++. I always programmed "in an object oriented way", but using function pointers instead of virtual methods.

At 15 I started to learn Pascal at school and to use Delphi. At 16 I started my first internship (using Delphi). At 18 I started to work professionally using C++ and since then I've developed my programming skills as a professional developer in C++ and C#, generally creating libraries that help other developers do their work easier, faster and with less errors.

Want more info or simply want to contact me?
Take a look at: http://paulozemek.azurewebsites.net/
Or e-mail me at: paulozemek@outlook.com

Codeproject MVP 2012, 2015 & 2016
Microsoft MVP 2013-2014 (in October 2014 I started working at Microsoft, so I can't be a Microsoft MVP anymore).

Comments and Discussions

 
GeneralThanks, and sorry for the commotion Pin
FDW2-Dec-11 7:14
FDW2-Dec-11 7:14 
GeneralIt is OK now. Pin
Paulo Zemek2-Dec-11 6:08
mvaPaulo Zemek2-Dec-11 6:08 
GeneralI was too hasty, and removed my comment: "There is nothing b... Pin
FDW2-Dec-11 6:05
FDW2-Dec-11 6:05 
GeneralNested locking as 'lock (po1) { lock (po2) { ... } }' may cr... Pin
FDW2-Dec-11 5:44
FDW2-Dec-11 5:44 
GeneralYes the code was (is) tested, but clearly not good enough Pin
FDW2-Dec-11 5:39
FDW2-Dec-11 5:39 
GeneralOoops, did not see that. I did fix the code. Thanks for spot... Pin
FDW2-Dec-11 5:38
FDW2-Dec-11 5:38 
GeneralSorry, but you are wrong. If the locks are always in the sa... Pin
Paulo Zemek2-Dec-11 5:33
mvaPaulo Zemek2-Dec-11 5:33 
General>> Imagine that the lock to po1 is true, but for po2 i... Pin
FDW2-Dec-11 5:31
FDW2-Dec-11 5:31 
GeneralThere is nothing buggy in the given code, it has been fully ... Pin
FDW2-Dec-11 5:28
FDW2-Dec-11 5:28 
GeneralNested locking as 'lock (po1) { lock (po2) { ... } }' will c... Pin
FDW2-Dec-11 5:26
FDW2-Dec-11 5:26 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.