PC Review


Reply
Thread Tools Rate Thread

Can two threads execute in the same lock region concurrently?

 
 
Russell Stuart
Guest
Posts: n/a
 
      6th May 2004
I always believed .Net monitors were a superset of the Java
synchronization mechanisms. The code below produces both
"Thread 1" and "Thread 2" lines, implying that two threads
are both executing in locked region guarded by the same
monitor. I thought that wasn't supposed to happen. Am I
wrong? It was compiled and run under VS 2003.

If you run it under the debugger then the behaviour changes -
only "Thread 1" lines are produced. This is what I expected
to happen in all cases.

using System;
using System.Threading;

public class Test {
Thread t1, t2;

public void Run1() {
Thread.Sleep(1000);
lock (this) {
Monitor.Pulse(this);
t2.Interrupt();
for (;
Console.WriteLine("Thread 1");
}
}

public void Run2() {
lock (this) {
try {
Monitor.Wait(this);
}
catch (ThreadInterruptedException) {
Console.WriteLine("2 Interrupted");
}
for (;
Console.WriteLine("Thread 2");
}
}

public void Go() {
t1 = new Thread(new ThreadStart(Run1));
t2 = new Thread(new ThreadStart(Run2));
t1.Start();
t2.Start();
}

public static void Main() {
new Test().Go();
}
}

 
Reply With Quote
 
 
 
 
Jon Skeet [C# MVP]
Guest
Posts: n/a
 
      6th May 2004
Russell Stuart <russell-(E-Mail Removed)> wrote:
> I always believed .Net monitors were a superset of the Java
> synchronization mechanisms. The code below produces both
> "Thread 1" and "Thread 2" lines, implying that two threads
> are both executing in locked region guarded by the same
> monitor. I thought that wasn't supposed to happen. Am I
> wrong? It was compiled and run under VS 2003.


Please see my reply in the C# newsgroup - there's no need to multipost.

--
Jon Skeet - <(E-Mail Removed)>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
 
Reply With Quote
 
Jon Skeet [C# MVP]
Guest
Posts: n/a
 
      6th May 2004
Jon Skeet [C# MVP] <(E-Mail Removed)> wrote:
> Russell Stuart <russell-(E-Mail Removed)> wrote:
> > I always believed .Net monitors were a superset of the Java
> > synchronization mechanisms. The code below produces both
> > "Thread 1" and "Thread 2" lines, implying that two threads
> > are both executing in locked region guarded by the same
> > monitor. I thought that wasn't supposed to happen. Am I
> > wrong? It was compiled and run under VS 2003.

>
> Please see my reply in the C# newsgroup - there's no need to multipost.


Sorry, of course it wasn't in the C# newsgroup. It was in this
newsgroup. So why the repeat post?

--
Jon Skeet - <(E-Mail Removed)>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
 
Reply With Quote
 
Russell Stuart
Guest
Posts: n/a
 
      6th May 2004
On Thu, 06 May 2004 08:33:10 +0100, Jon Skeet[ C# MVP] wrote:

> Sorry, of course it wasn't in the C# newsgroup. It was in this
> newsgroup. So why the repeat post?


Becuase I originally posted the query last night on 247. When
I came in this morning I couldn't see it - so I posted it
using a normal nntp client.

Then both posts appeared. Sorry for the confusion.

 
Reply With Quote
 
Jon Skeet [C# MVP]
Guest
Posts: n/a
 
      6th May 2004
Russell Stuart <russell-(E-Mail Removed)> wrote:
> On Thu, 06 May 2004 08:33:10 +0100, Jon Skeet[ C# MVP] wrote:
>
> > Sorry, of course it wasn't in the C# newsgroup. It was in this
> > newsgroup. So why the repeat post?

>
> Becuase I originally posted the query last night on 247. When
> I came in this morning I couldn't see it - so I posted it
> using a normal nntp client.
>
> Then both posts appeared. Sorry for the confusion.


Fair enough

--
Jon Skeet - <(E-Mail Removed)>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Lock Threads Analizer1 Microsoft C# .NET 2 7th Jan 2008 12:53 AM
DVD Region Lock =?Utf-8?B?U2FuZGFs?= Windows XP General 2 24th Feb 2007 12:46 AM
How to lock other threads jeff.ranney@gmail.com Microsoft C# .NET 5 26th Jan 2007 09:34 PM
XP Home region lock on DVD totaltomit Windows XP Hardware 5 26th Apr 2004 09:22 PM
how do I ensure only one instance of an application can execute concurrently Chris Anderson Microsoft Dot NET 3 16th Jan 2004 11:11 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 03:09 AM.