473,399 Members | 4,254 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,399 software developers and data experts.

Locking question

I have a function that can be called from multiple threads that sets some
static variables. To keep things consistent I am trying to use a lock
statement. Somehow two different threads are able to get inside the lock
statement at once. Here is the code

static object checkAndWaitLockObject = new object();
private static System.Threading.ManualResetEvent mre = new
System.Threading.ManualResetEvent(true);
private volatile static int threadID;

private void CheckAndWait()
{
lock(checkAndWaitLockObject)
{
if(!context.IsTransactionPending)
{
mre.Reset();
threadID = System.Threading.Thread.CurrentThread.GetHashCode( );
return;
}
else
{
if(threadID == System.Threading.Thread.CurrentThread.GetHashCode( ))
{
return;
}

mre.WaitOne();
mre.Reset();
}
}
}

If anyone has any ideas why the lock isn't working it would be appreciated.
Thanks, Scott
Nov 17 '05 #1
6 1258
Scott,

What actually happen? How do you know that two threads enter the lock?
--

Stoitcho Goutsev (100) [C# MVP]

"Scott S." <Scott S.@discussions.microsoft.com> wrote in message
news:89**********************************@microsof t.com...
I have a function that can be called from multiple threads that sets some
static variables. To keep things consistent I am trying to use a lock
statement. Somehow two different threads are able to get inside the lock
statement at once. Here is the code

static object checkAndWaitLockObject = new object();
private static System.Threading.ManualResetEvent mre = new
System.Threading.ManualResetEvent(true);
private volatile static int threadID;

private void CheckAndWait()
{
lock(checkAndWaitLockObject)
{
if(!context.IsTransactionPending)
{
mre.Reset();
threadID = System.Threading.Thread.CurrentThread.GetHashCode( );
return;
}
else
{
if(threadID == System.Threading.Thread.CurrentThread.GetHashCode( ))
{
return;
}

mre.WaitOne();
mre.Reset();
}
}
}

If anyone has any ideas why the lock isn't working it would be
appreciated.
Thanks, Scott

Nov 17 '05 #2
Scott S. wrote:
I have a function that can be called from multiple threads that sets some
static variables. To keep things consistent I am trying to use a lock
statement. Somehow two different threads are able to get inside the lock
statement at once. Here is the code


What evidence do you have that multiple threads are getting inside the
lock at the same time? I can't see how they would do so.

Do you have a short but complete program which demonstrates the
problem?

Jon

Nov 17 '05 #3
I'm debugging in Visual Studio. In one thread I'm on the threadID =
GetHashCode and on the other I'm on the if(!context.IsTransactionPending).
One time before that both threads said they were on the
if(!context.IsTransactionPending) at the same time. I'm hoping that the
debugger is wrong.
Nov 17 '05 #4
Scott,

I actually think that this code is going to block if the other threads
are making calls to it. Based on what you posted, the method is
sufficiently synchronized.

However, I think you will have a deadlock at some point if one thread
gets to the point where you are waiting for an event. Using events and
other synchronization objects inside of another sycnchronization object is a
disaster waiting to happen.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Scott S." <Scott S.@discussions.microsoft.com> wrote in message
news:89**********************************@microsof t.com...
I have a function that can be called from multiple threads that sets some
static variables. To keep things consistent I am trying to use a lock
statement. Somehow two different threads are able to get inside the lock
statement at once. Here is the code

static object checkAndWaitLockObject = new object();
private static System.Threading.ManualResetEvent mre = new
System.Threading.ManualResetEvent(true);
private volatile static int threadID;

private void CheckAndWait()
{
lock(checkAndWaitLockObject)
{
if(!context.IsTransactionPending)
{
mre.Reset();
threadID = System.Threading.Thread.CurrentThread.GetHashCode( );
return;
}
else
{
if(threadID == System.Threading.Thread.CurrentThread.GetHashCode( ))
{
return;
}

mre.WaitOne();
mre.Reset();
}
}
}

If anyone has any ideas why the lock isn't working it would be
appreciated.
Thanks, Scott

Nov 17 '05 #5
I only want one instance of this class created at any time. The
manualResetEvent is fired any time the class is committed, rolled back, or
disposed. In the destructor there is a Debug.Assert to confirm that one of
those three methods have been called so there shouldn't be any deadlocks.
Nov 17 '05 #6
Actually I've seen that before and I believe it's a "oops" in the debugger.
It seems when debugging lock statements, while the thread is "waiting" on
the lock, the debugger actually highlights the NEXT line (it highlights the
line that executes next - not the current line).

--
Adam Clauss

"Scott S." <Sc****@discussions.microsoft.com> wrote in message
news:4F**********************************@microsof t.com...
I'm debugging in Visual Studio. In one thread I'm on the threadID =
GetHashCode and on the other I'm on the if(!context.IsTransactionPending).
One time before that both threads said they were on the
if(!context.IsTransactionPending) at the same time. I'm hoping that the
debugger is wrong.

Nov 17 '05 #7

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

Similar topics

4
by: Michael Chermside | last post by:
Ype writes: > For the namespaces in Jython this 'Python internal thread safety' > is handled by the Java class: > > http://www.jython.org/docs/javadoc/org/python/core/PyStringMap.html > > which...
2
by: Geoffrey | last post by:
We have developed a python class that can read data files created from another application. These target files are C-ISAM files used for accounting applications so the "primary" application may be...
0
by: Steve McWilliams | last post by:
Hello, I am relatively new to MySql (4.0.14) but I have read through the relevent documentation and am still confused about how row level locking behaves with InnoDB tables. I created a...
16
by: Nid | last post by:
How do I do row-level locking on SQL Server? Thanks, Nid
2
by: spammy | last post by:
hi all, im trying to establish whether i have a race condition or critical section in the following. i have a dataaccess class that continually retireves a table from a sqlserver (which may be...
0
by: Megan | last post by:
Hi- I have a question about the Microsoft Access Record-Locking Information Icon. Whenever I, or my friends, open my MS Access database, the Microsoft Access Record-Locking Information Icon...
16
by: akantrowitz | last post by:
In csharp, what is the correct locking around reading and writing into a hashtable. Note that the reader is not looping through the keys, simply reading an item out with a specific key: If i...
6
by: shaanxxx | last post by:
I have global variable which is being shared between threads (problem is not connected with thread). Without using any mutex i have do some operation global variable in *consistent* way. ...
0
by: xpding | last post by:
Hello, I have a class MyEmbededList contains a generic dictionary, the value field is actually the MyEmbededList type as well. There is another class need to access and manipulate a list of...
15
by: Matt Brandt | last post by:
I am trying to get multiple threads to lock specific regions of a file. However, since each thread has the same PID, it appears that a lock by one thread does not block another thread from the same...
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?
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
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
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
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
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.