Click here to Skip to main content
13,199,685 members (65,944 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


Posted 2 Dec 2011

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))

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:
    // 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.


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


About the Author

Paulo Zemek
Engineer YouTube
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:
Or e-mail me at:

Codeproject MVP 2012, 2015 & 2016
Microsoft MVP 2013-2014 (now I work at Microsoft so I can't be a Microsoft MVP anymore)

You may also be interested in...


Comments and Discussions

GeneralThanks, and sorry for the commotion Pin
FDW2-Dec-11 7:14
memberFDW2-Dec-11 7:14 
GeneralIt is OK now. Pin
Paulo Zemek2-Dec-11 6:08
mvpPaulo Zemek2-Dec-11 6:08 
GeneralI was too hasty, and removed my comment: "There is nothing b... Pin
FDW2-Dec-11 6:05
memberFDW2-Dec-11 6:05 
GeneralNested locking as 'lock (po1) { lock (po2) { ... } }' may cr... Pin
FDW2-Dec-11 5:44
memberFDW2-Dec-11 5:44 
GeneralYes the code was (is) tested, but clearly not good enough Pin
FDW2-Dec-11 5:39
memberFDW2-Dec-11 5:39 
GeneralOoops, did not see that. I did fix the code. Thanks for spot... Pin
FDW2-Dec-11 5:38
memberFDW2-Dec-11 5:38 
GeneralSorry, but you are wrong. If the locks are always in the sa... Pin
Paulo Zemek2-Dec-11 5:33
mvpPaulo Zemek2-Dec-11 5:33 
Sorry, but you are wrong.

If the locks are always in the same order there is no chance of dead-lock... and that may be needed.
Usually that happens on different levels of locking. Imagine that you have a thread-safe list. Add and Remove do a lock.
But, at another place, you want to call Contains before and, if contains is false, add an item. The thread-safe list will be thread-safe at each add or at each contains... but between contains and Add, you may have another thread adding an item. So you will use another (external) lock.

In fact, you will always have:
External Lock - Internal lock (when using the Contains->Add).
Or you will have only internal locks. Surely having only external locks will work, but there are reasons to have two locks that way.

And I think your code was not rightly tested.
Create a situation where lock1 will work and lock2 will not. It will keep locking lock1 and not lock2... make it timeout... and then try to lock the lock1 by another thread. You will not be able to do it, as you lost a lot of locks.
General>> Imagine that the lock to po1 is true, but for po2 i... Pin
FDW2-Dec-11 5:31
memberFDW2-Dec-11 5:31 
GeneralThere is nothing buggy in the given code, it has been fully ... Pin
FDW2-Dec-11 5:28
memberFDW2-Dec-11 5:28 
GeneralNested locking as 'lock (po1) { lock (po2) { ... } }' will c... Pin
FDW2-Dec-11 5:26
memberFDW2-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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.171020.1 | Last Updated 25 Dec 2011
Article Copyright 2011 by Paulo Zemek
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid