Click here to Skip to main content
13,287,116 members (50,597 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

GeneralNested locking as 'lock (po1) { lock (po2) { ... } }' will c... Pin
FDW2-Dec-11 6:26
memberFDW2-Dec-11 6: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
Web03 | 2.8.171207.1 | Last Updated 25 Dec 2011
Article Copyright 2011 by Paulo Zemek
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid