Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / October 2006

Tip: Looking for answers? Try searching our database.

locks within locks

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
DaTurk - 31 Oct 2006 16:16 GMT
I have a question concerning nested locks.  I just noticed that I have
an object declared in my parent class that I use as the lock.  But what
I noticed is that in one of the childs methods, I lock that object,
then while still in the critical section I call one of my inherited
methods, and lock the same object.

So it's esentially this

object _lock = new object();

lock(_lock)
{
   lock(_lock)
   {

    //Do Something
 
    }
}

Is this OK to do?  It seems to be working I think.
Marc Gravell - 31 Oct 2006 16:26 GMT
Yes; locking structures are /generally/ re-entrant, meaning if you already
hold the lock you can effectively take out another... to avoid scenarios
with deadlocking yourself.

Marc
Brian Gideon - 31 Oct 2006 17:45 GMT
DaTurk,

Yep, that's fine because it is locking on the same object.

What you do need to look out for is the order of locks when different
objects are used.  A common deadlock scenario exists when you have two
different critical sections that lock on two different objects in
different orders.  There are plenty of others, but this is common
enough to mention.  For example, the following code would eventually
deadlock.

void CritialSection1()
{
 lock (a)
 {
   lock (b)
   {
     // whatever
   }
 }
}

void CritialSection2()
{
 lock (b)
 {
   lock (a)
   {
     // whatever
   }
 }
}

Brian

> I have a question concerning nested locks.  I just noticed that I have
> an object declared in my parent class that I use as the lock.  But what
[quoted text clipped - 17 lines]
>
> Is this OK to do?  It seems to be working I think.
Samuel R. Neff - 31 Oct 2006 20:10 GMT
While that works fine, I would be concerned with the fact that you
noticed the double-lock after the fact.  If you're not fully aware of
what is being locked when and what is already locked when certain code
is running, then it's more likely you'll create deadlock situations or
innefficient code (i.e., not a deadlock but code that's unnecessarily
waiting on other code).

In general it's a good practice to keep locked sections as small as
possible and be very aware of what is running inside locked sections.

Sam

------------------------------------------------------------
We're hiring!  B-Line Medical is seeking Mid/Sr. .NET
Developers for exciting positions in medical product
development in MD/DC.  Work with a variety of technologies
in a relaxed team environment.  See ads on Dice.com.

>I have a question concerning nested locks.  I just noticed that I have
>an object declared in my parent class that I use as the lock.  But what
[quoted text clipped - 17 lines]
>
>Is this OK to do?  It seems to be working I think.

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.