473,322 Members | 1,540 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,322 software developers and data experts.

Can two threads execute in the same lock region concurrently?

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();
}
}

Nov 22 '05 #1
4 1186
Russell Stuart <ru**********@stuart.id.au> 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 - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 22 '05 #2
Jon Skeet [C# MVP] <sk***@pobox.com> wrote:
Russell Stuart <ru**********@stuart.id.au> 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 - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 22 '05 #3
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.

Nov 22 '05 #4
Russell Stuart <ru**********@stuart.id.au> 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 - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 22 '05 #5

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

Similar topics

8
by: Eric B. | last post by:
Hi, I'm running into a slight programming issue here that I am hoping someone can help me with. I am developping a web-based PHP application, which means I have to consider multiple threads of...
4
by: Russell Stuart | last post by:
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...
2
by: Luk Vloemans | last post by:
Hey, Having a few problems using threads in C#. In the msdn documentation, mutual exclusion is only discussed when dealing with threads that all execute the same method. What's the actual...
1
by: Tim Smith | last post by:
Hi, I am so far familiar with the C# lock keyword, which I understand prevents any other thread from entering the portion of code which is locked, assuming you are locking the same object...
11
by: catsup | last post by:
Hi, I have an app written under version Python 2.3.5. The problem I'm having is that it hangs on one of its threads. The thread that hangs does updates to a standard dictionary shared with...
18
by: jess.austin | last post by:
hi, This seems like a difficult question to answer through testing, so I'm hoping that someone will just know... Suppose I have the following generator, g: def f() i = 0 while True: yield...
15
by: Bryce K. Nielsen | last post by:
I have an object that starts a thread to do a "process". One of the steps inside this thread launches 12 other threads via a Delegate.BeginInvoke to process. After these 12 threads are launched,...
3
by: Ritesh Raj Sarraf | last post by:
Hi, I have a small application, written in Python, that uses threads. The application uses function foo() to download files from the web. As it reads data from the web server, it runs a progress...
16
by: WATYF | last post by:
Hi there... I have a huge text file that needs to be processed. At the moment, I'm loading it into memory in small chunks (x amount of lines) and processing it that way. I'd like the process to be...
9
by: koschwitz | last post by:
Hi, I hope you guys can help me make this simple application work. I'm trying to create a form displaying 3 circles, which independently change colors 3 times after a random time period has...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
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: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
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
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.