473,395 Members | 1,403 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

locks within locks

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.

Oct 31 '06 #1
3 7644
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
Oct 31 '06 #2
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

DaTurk wrote:
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.
Oct 31 '06 #3

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.

On 31 Oct 2006 07:16:01 -0800, "DaTurk" <mm******@hotmail.comwrote:
>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.
Oct 31 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: rkusenet | last post by:
I am fairly new to SQL Server. I am writing a tool in stored procedure to identify locks in a table. I have already written the basic framework of the SP. It will reside in master database and take...
2
by: New MSSQL DBA | last post by:
hi all, we have a SQL2000SP3 runing in W2K3. The application is JDEdwards. recently I've observed that once in a while (about a few hours), there would be a process from the ERP application...
11
by: EoRaptor | last post by:
I`m exporting data from many Lotus Notes databases to a DB2 database using LotusScript. The LotusScript agents commit after EACH update/insert. Nevertheless, I keep getting transaction rollbacks on...
10
by: florian | last post by:
Hi, we have a contention problem because of an application which tries to insert a duplicate row in a table with primary key. This insert fails of course but the locks are not released within...
6
by: John Carroll | last post by:
Is there a SQL query that can be run against a database that will give me all the details on any locks that are in place at the given time? I am interested in find the lock type and owner. Thank...
4
by: Nick Barr | last post by:
Hi, I am trying to gather stats about how many times a resource in our web app is viewed, i.e. just a COUNT. There are potentially millions of resources within the system. I thought of two...
22
by: RayPower | last post by:
I'm having problem with using DAO recordset to append record into a table and subsequent code to update other tables in a transaction. The MDB is Access 2000 with the latest service pack of JET 4....
0
by: elphantasmo | last post by:
Hi, I have created a hosted AxWebBrowser control and I handle the NewWindow2 event to open new windows in my own forms. Our application accesses a JSF (Java Server Faces) backend with some...
5
by: Anja N | last post by:
Hi all, I have a asp.net web page that hosts some htcs with input boxes on it. The viewLinkContent and tabStop properties are defaulted to true so that users can add text and tab through the input...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.