473,385 Members | 1,521 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,385 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 5729
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...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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...
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...

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.