473,395 Members | 1,677 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,395 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 1189
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
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:
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...

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.