473,396 Members | 2,061 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,396 software developers and data experts.

Conceptual Problem with Monitor.Enter and Monitor.Exit

Dear Members,
I have a problem in the concepts of Monit.Enter and Monitor.Exit
(Before everything I should say that I know how to solve this problem
by

using Monitor.Wait and I do not need a solution. But this question
sticks to

my head as a conceptual problem)
Suppose there are two threads T1, T2 running concurrently:

T1()
{
While(true){
Monit.Enter(MyObjec);
{
....
}
Monitor.Exit(MyObjec);
}
}

T2()
{
While(true){
Monit.Enter(MyObjec);
{
....
}
Monitor.Exit(MyObjec);
}
}
Suppose T1 runs before T2 a little sooner.

After T1 gains the lock on MyObjec, T2 comes in and tries to get
access to

MyObjec too. Since T1 called Monitor.Enter(MyObjec) previously T2 will
be

blocked ( but as what MSDN says, it still remains in ready queue).
After a

while, T1 calls Monitor.Exit(MyObjec). So T2 can gain access to
MyObjec.

But surprisingly it cannot. Because T1 rapidly gains access to MyObjec
again.

It seems that while loop cycles too fast to let T2 gain the lock on
MyObjec. I

am not sure this is the matter of speed but I am sure that there is no
need

to call Monitor.Pulse(MyObjec) to notify T2 because T2 is still in
ready queue

(and Monitor.Pulse(MyObjec) just affects waiting queue).

What is the problem? Is this the matter of speed? Am I wrong about
the

concepts of Monitor methods?

Thanks in advance.
Jun 27 '08 #1
13 2649
Are you sure of your test? In the following, T2 wins every time (on my
machine, at least...).

Note that "setupReady" is used to guarantee our initial state - i.e.
T1 gets the lock and releases T2 (then does a short sleep to let T2
catch up), then tries to Enter (blocks), T1 releases the lock (Exit)
and immediately re-obtains it (Enter).

Marc

using System.Threading;
using System;
using System.Diagnostics;
static class Program
{
static void Main()
{
int t1 = 0, t2 = 0;
for (int i = 0; i < 1000; i++)
{
switch (RunTest())
{
case 1: t1++; break;
case 2: t2++; break;
}
}
Console.WriteLine("{0} vs {1}", t1, t2);
Console.ReadKey();
}
class WinnerStub
{
public volatile int Winner = 0;
}
static int RunTest()
{
WinnerStub sync = new WinnerStub();
ManualResetEvent setupReady = new ManualResetEvent(false),
haveAnswer = new ManualResetEvent(false);
ThreadPool.QueueUserWorkItem(delegate
{
Monitor.Enter(sync);
setupReady.Set();
Thread.Sleep(100);
// drop lock...
Monitor.Exit(sync);
Monitor.Enter(sync);
if (sync.Winner == 0)
{
sync.Winner = 1;
haveAnswer.Set();
}
Monitor.Exit(sync);
});
ThreadPool.QueueUserWorkItem(delegate
{
setupReady.WaitOne();
Monitor.Enter(sync);
if (sync.Winner == 0)
{
sync.Winner = 2;
haveAnswer.Set();
}
Monitor.Exit(sync);
});
haveAnswer.WaitOne();
Console.WriteLine("Winner: {0}", sync.Winner);
return sync.Winner;
}

}
Jun 27 '08 #2
(btw, I also tried a "while" setup like yours, but the results were
very even between the threads; so if you can post a short and complete
example that illustrates this?)
Jun 27 '08 #3
On Jun 16, 1:14 pm, Marc Gravell <marc.grav...@gmail.comwrote:
(btw, I also tried a "while" setup like yours, but the results were
very even between the threads; so if you can post a short and complete
example that illustrates this?)
Thank you for your attention. But you repeated what I told. My problem
is exactly what you mentioned: T2 always wins. But as you and I see in
my sample code (similarly in your code) one thread get the access and
then release it.
BUT:
Why the second thread can not get the access after that the first
thread released? As we know the second thread is on ready queue and
waiting eagerly for thread one to release!
Jun 27 '08 #4
Thank you for your attention. But you repeated what I told. My problem
is exactly what you mentioned: T2 always wins.
No; you said the exact opposite:
After a while, T1 calls Monitor.Exit(MyObjec). So T2 can gain access to
MyObjec. But surprisingly it cannot. Because T1 rapidly gains access to
MyObjec again. It seems that while loop cycles too fast to let T2 gain the
lock on MyObjec.
So it sounds like you are saying "T1 always wins".
Why the second thread can not get the access after that the first
thread released?
In my code the second thread (T2) *did* get access after the first
thread (T1) release, 100% of the time.

Hence: the best way to understand the difference is for you to post
something short but *complete* that demostrates what you are seeing.
Threading is one of those areas where the devil is *really* in the
detail.

Marc
Jun 27 '08 #5
On Jun 16, 2:25 pm, Marc Gravell <marc.grav...@gmail.comwrote:
Thank you for your attention. But you repeated what I told. My problem
is exactly what you mentioned: T2 always wins.

No; you said the exact opposite:
After a while, T1 calls Monitor.Exit(MyObjec). So T2 can gain access to
MyObjec. But surprisingly it cannot. Because T1 rapidly gains access to
MyObjec again. It seems that while loop cycles too fast to let T2 gain the
lock on MyObjec.

So it sounds like you are saying "T1 always wins".
Why the second thread can not get the access after that the first
thread released?

In my code the second thread (T2) *did* get access after the first
thread (T1) release, 100% of the time.

Hence: the best way to understand the difference is for you to post
something short but *complete* that demostrates what you are seeing.
Threading is one of those areas where the devil is *really* in the
detail.

Marc
Marc,
You are right and wrong!.
What I said in the first post is what I expected but never happened.
It is the sequence (which after reading MSDN) I expected but never
this ideal situation came true. I am investigating the reason. So:

1- Your result is true and exactly like me.
2- What I posted in the first post was what I designed and what I
expected to happen according to MSDN.
3- What I was expecting never happened at all (like what you
reported).
4- Now I am looking for the reason.

My actual code is not too different comparing with the simple bunch of
code I posted earlier. It is clear and can tell us everything needed.
So I think there is no need to post anything more. But if you insist I
post it.
Thanks again for your attention.

Jun 27 '08 #6
1- Your result is true and exactly like me.

Well, I really believe that we are seeing opposite behaviour.
2 - What I posted in the first post was what I designed and what I
expected to happen according to MSDN.
3- What I was expecting never happened at all (like what you
reported).
And I am saying that my code demonstrates exactly what you expected
from 2 - i.e. the second thread gets the lock. I did not report the
same findings as you in 3; I reported the opposite.

But if you are content...

Marc
Jun 27 '08 #7
On Jun 16, 5:04 pm, Marc Gravell <marc.grav...@gmail.comwrote:
1- Your result is true and exactly like me.

Well, I really believe that we are seeing opposite behaviour.
2 - What I posted in the first post was what I designed and what I
expected to happen according to MSDN.
3- What I was expecting never happened at all (like what you
reported).

And I am saying that my code demonstrates exactly what you expected
from 2 - i.e. the second thread gets the lock. I did not report the
same findings as you in 3; I reported the opposite.

But if you are content...

Marc
The second thread gets the lock but never releases it. Thread one
never gets the lock. Thats the problem.
Despite of which thread we are talking about, one thread always gets
the lock and never releases it. Why?
Jun 27 '08 #8
I think I may have confused you with the code. My example was to
contradict your specific claim that T2 never got the lock. With 2
threads running parallel, timing is important, but it isn't true that
one thread always keeps the lock; for example (going back to the
"while" example) I get pretty balanced results as follows:

T2: 46207
T1: 53794

With the code as below; this is two threads contending over a singel
lock, each incrementing a local counter, with 100k iterations (give-or-
take 1). Individual runs might be less balanced (or more balanced)
depending on what the OS is doing, the number of cores available,
karma, etc - but I would not expect to see 100k vs 0, or 0 vs 100k.

Marc

using System.Threading;
using System;
static class Program
{
static void Main()
{
ThreadPool.QueueUserWorkItem(DoWork, "T1");
ThreadPool.QueueUserWorkItem(DoWork, "T2");
Thread.Sleep(500); // let T1 & T2 get to evt.WaitOne
evt.Set();
Console.ReadKey();
}
static readonly ManualResetEvent evt = new
ManualResetEvent(false);
static readonly object sync = new object();
static int tally = 100000;
static void DoWork(object name)
{
int counter = 0;
evt.WaitOne(); // start both at the same time...
while (true)
{
Monitor.Enter(sync);
counter++;
Monitor.Exit(sync);

if(Interlocked.Decrement(ref tally) <= 0) break;
}
Console.WriteLine("{0}: {1}", name, counter);
}
}
Jun 27 '08 #9
And just in case the extra time for the Interlocked.Decrement was
changing things, I moved that inside the lock - but the behaviour is
still the same; updated "DoWork" follows, typical results:

T2: 53158
T1: 46843

static void DoWork(object name)
{
int counter = 0;
evt.WaitOne(); // start both at the same time...
while (true)
{
Monitor.Enter(sync);
counter++;
if (Interlocked.Decrement(ref tally) <= 0)
break;
Monitor.Exit(sync);
}
// broke while keeping the lock
Monitor.Exit(sync);
Console.WriteLine("{0}: {1}", name, counter);
}
Jun 27 '08 #10
I really do not know how to appreciate you but the original problem
still remains.
Let me clarify that I use VS .Net 2005 on Win XP SP2 and Intel CPU 2.4
MHZ.

I read your new code carefully.
This is what I deduced:

1- Two threads start at the same time.

2- Then one of them (depending on what OS does) enters the critical
section and gets the lock. The second one attempts to get the lock but
it will be blocks as other thread has not released the lock yet.

3-When the first thread (which has got the lock) releases the lock (by
calling Monitor.Exit) I expect that the second one (which was waiting
on "sysnc") now get the lock and increment the counter.

****
This is the problem! Can the second one accuires the lock before the
first one starts the next iteration?

If we write another code without "Interlocked.Decrement()" then this
new code will be exactly as the same as what I posted in the first
post. (I do not know really what Interlocked.Decrement does but I
insist on my question that why Entering and Existing are not enough)
So the code in my first post should run perfectly. But it does not.

Let me rephrase my question:
Is there enough time for OS to context switch and to give the control
to the waiting thread? (My experience with my own first code says no!)

So I can say that you solved the problem with something other than
Monitor.Exntr and Monitor.Exit(with "Interlocked.Decrement) . Am I
right?

Jun 27 '08 #11
On Jun 16, 5:09 pm, AliRezaGoogle <asemoo...@yahoo.comwrote:
I really do not know how to appreciate you but the original problem
still remains.
It would really help if you'd show us the problem in a complete
program then.

<snip>
This is the problem! Can the second one accuires the lock before the
first one starts the next iteration?

If we write another code without "Interlocked.Decrement()" then this
new code will be exactly as the same as what I posted in the first
post. (I do not know really what Interlocked.Decrement does but I
insist on my question that why Entering and Existing are not enough)
So the code in my first post should run perfectly. But it does not.
Your first post didn't include enough code to test anything.

Marc's post, by contrast, was a full program. The use of
Interlocked.Decrement was just to make sure that the loop (in each
thread) terminated. Each thread maintained a counter to show how many
times it had iterated. Interlocked.Decrement wasn't changing the
locking part at all.

If the second thread never had time to acquire the lock before the
first thread regained it, the numbers printed would be 100000 and 0.
Marc's numbers were fairly even, hence his contention that there isn't
a problem.

I doubt that it's *guaranteed* that the second thread will acquire the
lock - but I'd argue that any program whose correctness depended on
such a guarantee was far too brittle to start with.

Jon
Jun 27 '08 #12
On Jun 16, 7:59 pm, "Jon Skeet [C# MVP]" <sk...@pobox.comwrote:
On Jun 16, 5:09 pm, AliRezaGoogle <asemoo...@yahoo.comwrote:
I really do not know how to appreciate you but the original problem
still remains.

It would really help if you'd show us the problem in a complete
program then.

<snip>
This is the problem! Can the second one accuires the lock before the
first one starts the next iteration?
If we write another code without "Interlocked.Decrement()" then this
new code will be exactly as the same as what I posted in the first
post. (I do not know really what Interlocked.Decrement does but I
insist on my question that why Entering and Existing are not enough)
So the code in my first post should run perfectly. But it does not.

Your first post didn't include enough code to test anything.

Marc's post, by contrast, was a full program. The use of
Interlocked.Decrement was just to make sure that the loop (in each
thread) terminated. Each thread maintained a counter to show how many
times it had iterated. Interlocked.Decrement wasn't changing the
locking part at all.

If the second thread never had time to acquire the lock before the
first thread regained it, the numbers printed would be 100000 and 0.
Marc's numbers were fairly even, hence his contention that there isn't
a problem.

I doubt that it's *guaranteed* that the second thread will acquire the
lock - but I'd argue that any program whose correctness depended on
such a guarantee was far too brittle to start with.

Jon
Regarding to the code below my question is that why we have in out
put something like this:
Winer is T1
Winer is T1
Winer is T1
Winer is T2
Winer is T2
Winer is T2
Winer is T2
Winer is T2
Winer is T2
Winer is T1
Winer is T1
Winer is T1
Winer is T1
Winer is T1
......
......
......


class Program
{
static void Main()
{
Race r = new Race();
Thread t1 = new Thread(new ThreadStart(r.t1Proc));
Thread t2 = new Thread(new ThreadStart(r.t2Proc));

Thread.Sleep(500);

t1.Start();
t2.Start();
Console.ReadKey();
}
public class Race
{
Object SyncObj = new object();
int cntr = 0;
public void t1Proc()
{
while (true)
{
Monitor.Enter(SyncObj);
cntr++;
Console.WriteLine("Winer is {0} ", "T1", cntr);
Monitor.Exit(SyncObj);

if (cntr >= 10)
break;
//Thread.Sleep(700);
}

}
public void t2Proc()
{
while (true)
{
Monitor.Enter(SyncObj);
cntr++;
Console.WriteLine("Winer is {0} ", "T2", cntr);
Monitor.Exit(SyncObj);

if (cntr >= 10)
break;
//Thread.Sleep(700);
}
}
}
}
Jun 27 '08 #13
On Mon, 16 Jun 2008 23:40:56 -0700, AliRezaGoogle <as*******@yahoo.com>
wrote:
Regarding to the code below my question is that why we have in out
put something like this: [...]
You have that output because the first thread can accomplish more than one
iteration before Windows steps in and tells it that its current turn with
the CPU is up.

There's nothing fundamentally wrong with how the code you posted is
behaving. It's doing exactly what one would expect, given the relative
simplicity of the loop and the speed of a modern computer. Windows grants
CPU time to a particular thread in something like 50 millisecond blocks,
and it takes a lot less than 50 ms for the code you posted to get through
one iteration.

So, each thread gets to complete multiple iterations in a row, before the
other thread then gets its turn with the CPU.

Monitor.Wait() changes the behavior because it specifically waits (hence
the name). The "wait" causes the thread to yield the CPU, and thus the
other thread gets a chance to run. And in particular, the Monitor class
maintains a queue of threads waiting on the lock, and the thread that
called Wait() won't get the lock until every other thread that was already
in the queue before it gets a chance at the lock.

Other ways to change the behavior of your code would include simply
calling Thread.Sleep(1) (yields the CPU without specifically reentering
the queue for the lock), adding a spin-wait outside the section protected
by Monitor.Enter/Exit() (would cause the thread to use up its 50 ms
quantum without reaquiring the lock), and running the code on a computer
with more than one CPU (would allow the other thread to acquire the lock
as soon as the first thread released it, even if the first thread still
has CPU time left in its quantum).

Finally, some random thoughts on the code you posted:

-- There's no point in calling Thread.Sleep() from your main thread
after you create the Thread objects. You haven't started the threads, so
sleeping does absolutely nothing. Even if you had started the threads,
sleeping is pointless. The threads would still get to run eventually, and
not much later than you started them. And you could control your main
thread's coordination with the other threads in a much more precise,
useful way simply by calling Join() on each thread instance after you
start them.

-- There's no point in having two practically identical methods. Just
pass use the current thread's own ID, or pass some unique identifier to
the thread procedure, where there's just a single thread procedure used by
both threads.

-- If you do go back and modify the code for any reason, you might as
well fix it so that the word "winner" is spelled correctly. Maybe even
change the code so that the word is _used_ correctly too. :)

Pete
Jun 27 '08 #14

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

Similar topics

5
by: ste | last post by:
this code not work: it is no possible Exit from Monitor Why ? thank ste //////// using System; using System.Collections;
11
by: Michi Henning | last post by:
Hi, I'm calling Monitor.Wait() from a console event handler. It's not working -- the call to Wait() immediately causes the process to exit. Is it impossible for some reason to call Wait() from...
2
by: Shawn B. | last post by:
Greetings, What is the difference between lock(...) and Monitor.Enter(...) / Monitor.Exit(...) ? Thanks, Shawn
5
by: Ken Varn | last post by:
If I have a value type such as int that I want to protect in a multi-threaded situation, Is it safe to use Monitor::Enter(__box(value))? I am thinking that a different object pointer is generated...
8
by: Michael Kennedy | last post by:
Hi, I have been looking into the claim that the keyword lock is not safe when exceptions are possible. That lead me to try the following code, which I think has uncovered a serious error in the...
6
by: Clark Sann | last post by:
Can someone help me understand what object should be used as the lock object? I've seen some programs that use Monitor.Enter(Me). Then, in those same programs, they sometimes use another object. ...
2
by: www.brook | last post by:
I have a VC++.net code. Two threads are created, one thread keeps appending elements to the queue, another keeps deleting an element from the queue. I tried to use monitor, but it seems that the...
0
by: fiefie.niles | last post by:
I am having problem with thread. I have a Session class with public string variable (called Message) that I set from my Main program. In the session class it checks for the value of Message while...
2
by: Ian | last post by:
Monitor::Enter and Monitor::Exit must be called in pairs. Is there a way to determine if Monitor::Exit has been called so that it does not get called a second time? The psuedocode below...
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
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...
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,...

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.