Click here to Skip to main content
Click here to Skip to main content
Alternative Tip

Locking multiple (up to 2) objects

, 25 Dec 2011
Rate this:
Please Sign up or sign in to vote.
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:
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:
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)

About the Author

Paulo Zemek
Architect
Canada Canada
I started to program computers when I was 11 years old, as a hobbist, 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 they 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
Microsoft MVP 2013

Comments and Discussions

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

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

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

| Advertise | Privacy | Mobile
Web03 | 2.8.140718.1 | Last Updated 25 Dec 2011
Article Copyright 2011 by Paulo Zemek
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid