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

Does Monitor.Exit yield to waiting threads?

Hi,

Consider this code:

----
Monitor.Pulse(oLock);
Monitor.Exit(oLock);
----

If a thread was waiting on oLock then will the current thread
immediately yield following the call to Exit() to allow other
threads(including the thread that was waiting) to do some work? Or will
they have to wait to be handed a timeslice according to the scheduling
algorithm's normal operation?

Thus if I wanted to give the waiting thread a greater chance of
obtaining a time slice as soon as the lock is released I was thinking
of doing this:

----
Monitor.Pulse(oLock);
Monitor.Exit(oLock);
Thread.Sleep(0); // yield to another thread.
----
I realise this may be picking hairs but I have a CPU bound worker
thread that interacts closely with a GUI [thread] and to prodcue a
smoother user experience I have used Thread.Sleep(0) in a few places to
allow the GUI thread to process its message queue more often than
normal, this works very nicely. Reducing the worker thread's priority
helped a little but not as much as a few carefully placed calls to
Thread.Sleep(0). Now I have one scenario where the GUI thread is very
briefly waiting on the worker thread, and I'm wondering if I should
yield to get the absolute best resposiveness or whether this happens
anyway.

Thanks,

Colin Green

Oct 12 '05 #1
5 3815
For the record I had a think about this and came up with the following
piece of code to test what is happening:

---------------------------
class Class1
{
volatile int counter=0;
int snapshot;
object oLock = new object();

[STAThread]
static void Main(string[] args)
{
Class1 oClass1 = new Class1();
oClass1.Run();
}

public void Run()
{
Thread oThread1 = new Thread(new ThreadStart(ThreadOne));
Thread oThread2 = new Thread(new ThreadStart(ThreadTwo));

oThread1.Start();
oThread2.Start();
}

private void ThreadOne()
{
Monitor.Enter(oLock);

for(counter=0; counter<10000000; counter++)
{
if(counter==5000000)
{
Monitor.Pulse(oLock);
Monitor.Exit(oLock);
Thread.Sleep(0);
}
}
}

private void ThreadTwo()
{
// Wait for thread 1 to signal us.
Monitor.Enter(oLock);

snapshot = counter;

Monitor.Pulse(oLock);
Monitor.Exit(oLock);

Console.WriteLine("counter=" + snapshot);
}
}
---------------------------

Basically ThreadOne() counts to 10 million and releases a lock at 5
million. ThreadTwo() waits for the lock to be released and takes a
snapshot of the counter. Without the call to Thread.Sleep(0) the
snapshot value is typically around 5,000,2500
plus or minus a few hundred, just occasionally returning exactly
5,000,000.

With the call to Thread.Sleep(0) the snapshot value is always
5,000,000. I realise that without being explicitly stated in the CLR
spec or whatever that this sort behaviour may be implememtation or
platform specific, e.g. does the Mono runtime work this way? But for
now Thread.Sleep(0) works for me!

Colin.

Oct 13 '05 #2
Colin,

I've seen nothing in the documentation that indicates that Monitor.Exit
will relinquish the remainder of its time slice to other threads nor
have I observed behavior that suggests it is happening.

Brian

bu*******@hotmail.com wrote:
Hi,

Consider this code:

----
Monitor.Pulse(oLock);
Monitor.Exit(oLock);
----

If a thread was waiting on oLock then will the current thread
immediately yield following the call to Exit() to allow other
threads(including the thread that was waiting) to do some work? Or will
they have to wait to be handed a timeslice according to the scheduling
algorithm's normal operation?

Thus if I wanted to give the waiting thread a greater chance of
obtaining a time slice as soon as the lock is released I was thinking
of doing this:

----
Monitor.Pulse(oLock);
Monitor.Exit(oLock);
Thread.Sleep(0); // yield to another thread.
----
I realise this may be picking hairs but I have a CPU bound worker
thread that interacts closely with a GUI [thread] and to prodcue a
smoother user experience I have used Thread.Sleep(0) in a few places to
allow the GUI thread to process its message queue more often than
normal, this works very nicely. Reducing the worker thread's priority
helped a little but not as much as a few carefully placed calls to
Thread.Sleep(0). Now I have one scenario where the GUI thread is very
briefly waiting on the worker thread, and I'm wondering if I should
yield to get the absolute best resposiveness or whether this happens
anyway.

Thanks,

Colin Green


Oct 13 '05 #3
Hi Brian,

Indeed, but sometimes it's unclear as to whether some action is implied
by the nature of a method. So in this particular case perhaps the
documentation should state that the current timeslice will not
explicitly be relinquished following an Exit().

Regards,

Colin.

Oct 14 '05 #4
Colin,

I'd say a lot of the time the behavior of a method is unclear because
the documentation is so coarse in some areas, especially in the area of
threading. So, I do see where you're coming from.

Brian

bu*******@hotmail.com wrote:
Hi Brian,

Indeed, but sometimes it's unclear as to whether some action is implied
by the nature of a method. So in this particular case perhaps the
documentation should state that the current timeslice will not
explicitly be relinquished following an Exit().

Regards,

Colin.


Oct 14 '05 #5
<bu*******@hotmail.com> wrote:
Indeed, but sometimes it's unclear as to whether some action is implied
by the nature of a method. So in this particular case perhaps the
documentation should state that the current timeslice will not
explicitly be relinquished following an Exit().


But at that point, the class wouldn't be flexible - MS would be
*forced* never to explicitly relinquish the timeslice following a call
to Exit(), even if they later decided it would be a good idea.

There's always a very difficult balance between specifying *exactly*
what a method does, so that its behaviour can be more closely
predicted, and leaving it loose enough to allow for better alternative
implementations which still achieve the same main aim in the future.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Oct 14 '05 #6

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

Similar topics

4
by: Vinay C | last post by:
Hi, Can anyone clear me that, when should we use go for mutex, and in which situation should we opt for monitor, lock, semaphone and other objects, in a multithreaded application for synchronization...
25
by: vooose | last post by:
Suppose execution of a particular thread T1 hits Monitor.Enter(obj); //critical section and blocks at the first line. (ie someone else is in the critical section) Now suppose more threads...
4
by: Charles Law | last post by:
I've been using monitors a bit lately (some of you may have heard ;-) ) and then up pop Manual and AutoResetEvents , and they look for all the world like the same thing. Are they...
5
by: bughunter | last post by:
Hi, Consider this code: ---- Monitor.Pulse(oLock); Monitor.Exit(oLock); ---- If a thread was waiting on oLock then will the current thread
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...
2
by: DeveloperX | last post by:
Hi, Below is a hacked up version of an example I found on MSDN. My only change was to add a third thread and some Sleeps. That's where my question(s) comes in. In the original it seems to work...
1
by: ShadowLocke | last post by:
This is an HTML application that allows you to monitor the CPU Usage (as seen in task manager) for any one task either on the local computer or a remote computer using vbscript. It then alerts you...
13
by: AliRezaGoogle | last post by:
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...
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
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
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...

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.