473,765 Members | 2,070 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Monitor unblock priority

Suppose execution of a particular thread T1 hits

Monitor.Enter(o bj);
//critical section

and blocks at the first line. (ie someone else is in the critical
section) Now suppose more threads T2, T3... try to enter the critical
section and are blocked.

What is the order that the threads get to enter the critical section?
I'm hoping its

T1, T2, T3...

If not, is there any (easy) way to guarantee the thread that blocked
first gets to enter the CS next?
--
Wal
http://www.vooose.com

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 16 '05 #1
25 2708
Inline ***

Willy.

"vooose" <no****@microso ft.com> wrote in message
news:ut******** ******@TK2MSFTN GP14.phx.gbl...
Suppose execution of a particular thread T1 hits

Monitor.Enter(o bj);
//critical section

and blocks at the first line. (ie someone else is in the critical
section) Now suppose more threads T2, T3... try to enter the critical
section and are blocked.

What is the order that the threads get to enter the critical section?
I'm hoping its

T1, T2, T3...
*** No, there is no way to hope for a FIFO fashion in thread scheduling in
..NET.
If not, is there any (easy) way to guarantee the thread that blocked
first gets to enter the CS next?
*** No, there is no way at all. What's more, thread "fairness" is not
guaranteed in .NET. That means that it's possible, when you have multiple
threads that are constantly trying to take ownership of an object, that some
thread(s) will never get serviced at all.

--
Wal
http://www.vooose.com

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!

Nov 16 '05 #2
Hi
I have been searching but couldn't find any article or document that says
it is implemented that way . but you can use semaphore objects or at worst
case build your own queue where blocked threads check in and out.
Mohamed Mahfouz
Developer Support Engineer
ITWorx on behalf of Microsoft EMEA GTSC

Nov 16 '05 #3
Willy, thanks for your replies.

I have thought of perhaps a way to guarantee the order of execution, not
using monitors. Instead of

Monitor.Enter(o bj);

and block subsequent threads, you do (pseudo here)

myArrayList.Add (Thread.Current Thread);
while(myArrayLi st[0] != Thread.CurrentT hread)
Thread.Sleep(10 );

//got past loop, do critucal stuff

myArrayList.Rem ove(Thread.Curr entThread);

Naturally the ArrayList would be made thread safe.

Any thoughts there?

Also I was wondering if you could answer one more question re: Monitors.
When we do

Monitor.Enter(o bj);

What happens if the Thread calls sleep? Naturally I would expect the
other threads to be locked out until the thread wakes up and calls Exit(
) but I'm not sure if this is what is guaranteed to happen.
--
Wal
http://www.vooose.com

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 16 '05 #4
vooose <no****@microso ft.com> wrote:
I have thought of perhaps a way to guarantee the order of execution, not
using monitors. Instead of

Monitor.Enter(o bj);

and block subsequent threads, you do (pseudo here)

myArrayList.Add (Thread.Current Thread);
while(myArrayLi st[0] != Thread.CurrentT hread)
Thread.Sleep(10 );

//got past loop, do critucal stuff

myArrayList.Rem ove(Thread.Curr entThread);

Naturally the ArrayList would be made thread safe.

Any thoughts there?
Yes - it's nastily inefficient, as you could have lots of threads which
end up taking most of the time calling Sleep. Instead, use a monitor
and Wait on it, then call NotifyAll on the monitor when you come out of
the appropriate block. (There's a bit more to it that that, of course,
but that's the relevant bit.)
Also I was wondering if you could answer one more question re: Monitors.
When we do

Monitor.Enter(o bj);

What happens if the Thread calls sleep? Naturally I would expect the
other threads to be locked out until the thread wakes up and calls Exit(
) but I'm not sure if this is what is guaranteed to happen.


Yes. Calling Thread.Sleep doesn't release any monitors. Calling
Monitor.Wait releases the monitor you wait on.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #5

"Jon Skeet [C# MVP]" <sk***@pobox.co m> wrote in message
news:MP******** *************** *@msnews.micros oft.com...
vooose <no****@microso ft.com> wrote:
I have thought of perhaps a way to guarantee the order of execution, not
using monitors. Instead of

Monitor.Enter(o bj);

and block subsequent threads, you do (pseudo here)

myArrayList.Add (Thread.Current Thread);
while(myArrayLi st[0] != Thread.CurrentT hread)
Thread.Sleep(10 );

//got past loop, do critucal stuff

myArrayList.Rem ove(Thread.Curr entThread);

Naturally the ArrayList would be made thread safe.

Any thoughts there?


Yes - it's nastily inefficient, as you could have lots of threads which
end up taking most of the time calling Sleep. Instead, use a monitor
and Wait on it, then call NotifyAll on the monitor when you come out of
the appropriate block. (There's a bit more to it that that, of course,
but that's the relevant bit.)


Jon,

And what if the CurrentThread never equals the myArrayList[0] thread?
This what I meant in my previous reply, don't count on it, the CLR doesn't
guarantee "fairness", so it's possible the thread your waiting for never get
serviced.
AFAIK there is no solution to this in managed world.

Willy.
Nov 16 '05 #6
Eventually CurrentThread would have to equal myArrayList[0] (all other
things working correctly).

Eventually thread T2 that is waiting on T1 to remove itself from
myArrayList so T2 is now == myArrayList[0].

When T2 wakes up from Thread.Sleep(10 ); it entera the CS.
How could T2 not be guaranteed that execution sequence?

To me that is like saying any given thread may never execute one line of
code because the OS never schedules that thread to run!

--
Wal
http://www.vooose.com

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 16 '05 #7

"vooose" <no****@microso ft.com> wrote in message
news:OR******** *****@TK2MSFTNG P11.phx.gbl...
Eventually CurrentThread would have to equal myArrayList[0] (all other
things working correctly).

Eventually thread T2 that is waiting on T1 to remove itself from
myArrayList so T2 is now == myArrayList[0].

When T2 wakes up from Thread.Sleep(10 ); it entera the CS.
How could T2 not be guaranteed that execution sequence?

To me that is like saying any given thread may never execute one line of
code because the OS never schedules that thread to run!


*** Wait a minute I didn' say that your threads don't get scheduled, I said
this isn't guaranteed under the CLR, and you should never expect they are
scheduled in a FIFO order (or LIFO or any order at all). That's why it's
called "unfair thread synchronization ", and when your application absolutely
requires a fair thread synchronization (in a certain order) you shouldn't
use the .NET framework at all.

Why is that?
- When the CLR decides to kick a GC run, it determines which threads are
executing managed code and which are executing unmanaged code. When done the
CLR suspends the managed threads, unmanaged threads are treated specially
and can continue to run as long as they don't try to return to managed code.
Whenever the OS suspends a thread, it stops the thread waiting for a
synchronization object, later when the thread resumes, all suspended threads
start a race to obtain ownership of the synchronization object that it was
waiting on before the suspend. The result is that the threads (waiting for a
certain synchronization object) loose their order in the wait queue, and
loosing their order can possibly mean they don't get serviced for a long
period or worse they don't get scheduled at all.

- Other .NET API's that re-queue the wait order are WaitAll, WaitAny and
WaitOne.

Willly.
Nov 16 '05 #8
Willy Denoyette [MVP] <wi************ *@pandora.be> wrote:
And what if the CurrentThread never equals the myArrayList[0] thread?
*One* of the ones which is unblocked by a call to PulseAll (sorry about
using the Java terminology before!) will be. Then everything moves down
the list (a queue would be better than an ArrayList here) and next time
the next thread gets a go.
This what I meant in my previous reply, don't count on it, the CLR doesn't
guarantee "fairness", so it's possible the thread your waiting for never get
serviced.
AFAIK there is no solution to this in managed world.


There is, and it can be encapsulated, I'm sure. It's probably not a bad
thing to write a utility class for - I'll give it a go when I get some
time.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #9
Jon, you said:

<quote>
Yes - it's nastily inefficient, as you could have lots of threads which
end up taking most of the time calling Sleep. Instead, use a monitor
and Wait on it, then call NotifyAll on the monitor when you come out of
the appropriate block.
</quote>

Regarding the use of

while(myArrayLi st[0] != Thread.CurrentT hread)
Thread.Sleep(10 );

Lets say the average thread has to wait 50ms, so makes the call 5 times.
I'm not sure of the overhead of that versus the overhead of something
like

Monitor.Enter( )

I could see three things going on.

a) the waiting thread goes to sleep and is somehow woken up by the
locking thread calling Monitor.Exit( )

b) the waiting thread constantly checks to see if it can Enter( ).

c) somewhere in between, where it sleeps for a short period then checks
- something like what I coded above.
Obviously the best way would be a) but I'm not even sure it works like
this in the first place.

If indeed it *does* work this way, then I suppose instead of doing

while(myArrayLi st[0] != Thread.CurrentT hread)
Thread.Sleep(10 );

you could add it to a FIFO and somehow get it to sleep indefinitly (how
so?). Then when the CS thread exits it wakes the thread at the front of
the queue

--
Wal
http://www.vooose.com

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 16 '05 #10

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

Similar topics

5
3851
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
2
3310
by: Jack David | last post by:
Using the code below I am able to monitor a single directory for a new file and then kick-off a process to deal with the file. The question is??? How would I modify this code to be able to monitor a couple of different directories and based upon the directory where the new file is created kick-off a process Example: File A in Directory B starts process C
4
5905
by: Vittorio Pavesi | last post by:
Hello, I'm using CDO in my VB Application. Does anybody know how to set the Mail priority (Urgent, Medium, Low) ?? Thanks Vittorio ---------------------------------------------------------------------------- -------------------------------- Const cdoSendUsingMethod = "http://schemas.microsoft.com/cdo/configuration/sendusing"
1
5199
by: Kevin | last post by:
In a newsgroup thread from Jan 8, 2003 between Barry Holsinger and the VBDotNet Team, please review this excerpt: "You understood my problem completely. Your sample code provides a really elegant way to inject CrLf into the input stream, which effectively unblocks the ReadLine method. Last night, I had finally got the WriteConsoleInput
1
1667
by: Mis Dep. | last post by:
Dear all how can i monitor web service and check user connect from web service to sql server Brg , TingN@ng
1
4873
by: DR | last post by:
What ports do i need to unblock on client and server (running msvsmon.exe) to debug remotely from my client box with visual studio 2005 pro? When I attach to remote process a connection shows up in msvsmon.exe on the remote machine, however, the client box with visual studio displays error: "Unable to connect to the mricosoft visual studio remtoe debugging monitor named 'the box name' the micorosft visual studio remote debugging monitor...
0
9568
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9404
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10007
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9835
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8833
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5277
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3926
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3532
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2806
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.