473,396 Members | 2,013 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,396 software developers and data experts.

using lock

Hello,

Why doesn't the lock in Class2::Execute protect the critical section:

public Class1
{
...
public DataTable SchemaTable = new DataTable();
}

Class1 creates many instances of Class2 and calls the corresponding
Execute function on different threads.

public Class2
{
...
public void Execute()
{

// sync multiple threads - ensure only one thread obtains the
schema table
// does not really work, many threads enter this section at the
same time.
lock (_parent.SchemaTable)
{
if (0 == _parent.SchemaTable.Count)
{
_parent.SchemaTable = rdr.GetSchemaTable(); // returns a new
DataTable
}
}
...
}
...
}

I think the problem is that rdr.GetSchemaTable returns a new DataTable
that _parent.SchemaTable points at but I am not really clear how the
..NET internals work. I am able to get around this problem by using a
dedicated SyncLock object.

I would appreciate if someone could point out clearly why the code
should not be expected to work.

Thanks

Nov 17 '05 #1
3 5731
marfind,

Yeah, this is definitely not good. A lock statement basically compiles
to the following:

// If you use lock (this) ;
Montior.Enter(this);

try
{
}
finally
{
Monitor.Exit(this);
}

So, by changing the contents of the reference that was passed to enter,
you never actually release the lock. In effect, you are allocating all of
these locks, which are never released, but every time you make a call, you
change the value of what is locked on the next call. That's why it never
works.

I'm surprized the compiler didn't catch this (it could be because you
are locking on something that is returned from the object).

In the end, you are on the right track, using a dedicated sync lock
object for the lock.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com
"marvind" <ma********@yahoo.com> wrote in message
news:11*********************@o13g2000cwo.googlegro ups.com...
Hello,

Why doesn't the lock in Class2::Execute protect the critical section:

public Class1
{
...
public DataTable SchemaTable = new DataTable();
}

Class1 creates many instances of Class2 and calls the corresponding
Execute function on different threads.

public Class2
{
...
public void Execute()
{

// sync multiple threads - ensure only one thread obtains the
schema table
// does not really work, many threads enter this section at the
same time.
lock (_parent.SchemaTable)
{
if (0 == _parent.SchemaTable.Count)
{
_parent.SchemaTable = rdr.GetSchemaTable(); // returns a new
DataTable
}
}
...
}
...
}

I think the problem is that rdr.GetSchemaTable returns a new DataTable
that _parent.SchemaTable points at but I am not really clear how the
.NET internals work. I am able to get around this problem by using a
dedicated SyncLock object.

I would appreciate if someone could point out clearly why the code
should not be expected to work.

Thanks

Nov 17 '05 #2
yes, it does help. Thank you.

Nov 17 '05 #3
Nicholas Paldino [.NET/C# MVP] <mv*@spam.guard.caspershouse.com> wrote:
Yeah, this is definitely not good. A lock statement basically compiles
to the following:

// If you use lock (this) ;
Montior.Enter(this);

try
{
}
finally
{
Monitor.Exit(this);
}

So, by changing the contents of the reference that was passed to enter,
you never actually release the lock.


No, that's not true.

According to the spec:

<quote>
A lock statement of the form

lock (x) ...

where x is an expression of a reference-type, is precisely equivalent
to

System.Threading.Monitor.Enter(x);
try {
...
}
finally {
System.Threading.Monitor.Exit(x);
}

except that x is only evaluated once.
</quote>

Note the "x is only evaluated once" bit.

The lock is correctly released, but it won't be the same lock that is
acquired by the next call.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #4

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

Similar topics

9
by: Simon Harvey | last post by:
Hi all, In my project I have made a number of helper methods static. As I understand it, this will create the problem that multiple threads could access the static method at the same time and...
0
by: Bruce Pullen | last post by:
DB2 v7.2 (FP7 - DB2 v7.1.0.68) on AIX 5.2.0.0. We're seeing unexpected single row (then commit) insert locking behaviour. We're seeing Applications that already hold row-level W locks in...
11
by: Doug Thews | last post by:
I've been working on some samples that use BeginInvoke/EndInvoke. In one example, I call BeginInvoke and pass it an AsyncCallback function pointer. I was messing around with ReaderWriterLocks and...
1
by: Jim S | last post by:
My company's code often includes locks, using statements, and try/catch blocks and ends up with a lot of brackets. Would it make sense for C# to allow multiple items in these types of blocks? ...
3
by: Tony Johansson | last post by:
Hello! We have MFC application from VC6. Assume we compile the MFC application in VC8. Does anything special be done to the MFC application accept remove all the errors before being able to use...
0
by: sang | last post by:
Hi I want to lock the table with both read and write there is no action held after lock the table. I know how to lock the table with read and write, lock table mytable read; lock table...
1
by: satish mullapudi | last post by:
Hi all, I am using DB2 v 8.2 , Win XP OS. I have creaed a sample situation which results in a deadlock created by appl1 & appl2.Now, I want to release one of the locks (using CLP, not Control...
25
by: indrawati.yahya | last post by:
OK, before you all stab me with your OT pitchfork, I just want to mention that this question is indeed related to c.l.c++. My question is, does the well-known method to swap two integers produce...
19
by: =?ISO-8859-1?Q?Nordl=F6w?= | last post by:
I am currently designing a synchronized queue used to communicate between threads. Is the code given below a good solution? Am I using mutex lock/unlock more than needed? Are there any resources...
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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.