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

Threading priority

Consider a Server that spawns child threads to handle requests,
something of the form:

ThreadPool.QueueUserWorkItem(new WaitCallback(MyClass.HandleRequest));

and HandleRequest would look something like

public void HandleRequest(Object stateInfo)
{
//first section (lots of code)

//critical section

//end method
}

Suppose two threads are in operation, T1 and T2, where T1 was created
FIRST. Is there any way to guarantee that T1 enteres the critical
section before T2?

Suppose T1 was in the 'first section' and then T2 got control and
somehow scheduling allowed it to 'get in' to the critical section before
T1.

I guess it matters not how long the first section of code is, because at
any instance T2 could enter the Run state and beat T1 to the CS.

--
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
4 1255
It sounds to me like you need to put the first section inside the
critical section as well. At least some of it. Unfortunately that in
essence makes the whole worker thread method synchronized, which may not
be what you want.

There is actually no guarantee that T1 starts executing code in the
HandleRequest method before T2 *at all*.

Regards,
Joakim

vooose wrote:
Consider a Server that spawns child threads to handle requests,
something of the form:

ThreadPool.QueueUserWorkItem(new WaitCallback(MyClass.HandleRequest));

and HandleRequest would look something like

public void HandleRequest(Object stateInfo)
{
//first section (lots of code)

//critical section

//end method
}

Suppose two threads are in operation, T1 and T2, where T1 was created
FIRST. Is there any way to guarantee that T1 enteres the critical
section before T2?

Suppose T1 was in the 'first section' and then T2 got control and
somehow scheduling allowed it to 'get in' to the critical section before
T1.

I guess it matters not how long the first section of code is, because at
any instance T2 could enter the Run state and beat T1 to the CS.

--
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
Thanks for your reply. You are saying that as soon as T1.Start() is
called, and then T2.Start() after, T2 could even execute its first line
of code before T1?

ie even putting a lock on the very first line won't help me!

--
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 #3
You could pass T1 an event that it signals as soon as it has reached a
safe point in your thread proc. Let the main thread wait for that event
before it starts T2.

It's hard to say without knowing your exact problem.

Joakim,
Regards

vooose wrote:
Thanks for your reply. You are saying that as soon as T1.Start() is
called, and then T2.Start() after, T2 could even execute its first line
of code before T1?

ie even putting a lock on the very first line won't help me!

--
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 wrote:
Thanks for your reply. You are saying that as soon as T1.Start() is
called, and then T2.Start() after, T2 could even execute its first line
of code before T1?

ie even putting a lock on the very first line won't help me!


If you need inter-thread synchronization, you can't rely on critical
sections, you need to use signalling.

If your T1 and T2 threads are related, in such a way that T1 absolutely
must execute some piece of code before T2 get to it, then you need to
add code to both threads that guarantee that. For instance, you could
create an event object that is initially unsignalled, let T2 wait for
the event to become signalled, and let T1 signal the event when it has
completed the block of code that has to execute before T2 continues.

Throwing a few items into the thread pool will not in any way guarantee
anything about their order. Sure, the thread pool might be implemented
in such a way that it doles out the work objects to the threads in the
order it receives them, but the way the thread system works will make
these threads seem to run pretty random, ie. T1 will execute a bit, T2
will execute a bit, T7 will execute a bit, T5 will execute a bit, etc.

--
Lasse Vågsæther Karlsen
http://www.vkarlsen.no/
mailto:la***@vkarlsen.no
PGP KeyID: 0x0270466B
Nov 16 '05 #5

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

Similar topics

19
by: Jane Austine | last post by:
As far as I know python's threading module models after Java's. However, I can't find something equivalent to Java's interrupt and isInterrupted methods, along with InterruptedException....
4
by: Joe Wong | last post by:
Hi, Is there any way to increase/decrease a thread priority in Python? Best regards, -- Wong
77
by: Jon Skeet [C# MVP] | last post by:
Please excuse the cross-post - I'm pretty sure I've had interest in the article on all the groups this is posted to. I've finally managed to finish my article on multi-threading - at least for...
4
by: Bardo | last post by:
Hi, I have a situation where I am capturing both a WMI event utilising the "ManagementEventWatcher" in the "System.Management" namespace, and a corresponding event ("EntryWritten") raised from...
1
by: craigedmunds | last post by:
Was having a few problems with some threading issues following the microsoft support webcast Microsoft ASP.NET Threading. Spoke to Wade Mascia & managed to get clarification. Thought i'd share it...
7
by: IcedCrow | last post by:
I have code: Dim th as new threading.thread(addressof MyMethod) th.start() If th.threadstate = threadstate.running then th.priority = threading.threadpriority.abovenormal End If My question...
9
by: AdrianJMartin | last post by:
Hi all, I have a need for a STA thread from asp.net. I can create the thread and it runs fine. But when it is finished, the thread still 'hangs' arround. Visible only to the debugger..... I get...
4
by: Lee Moore | last post by:
I am trying to develop a threaded function to create a dataset from an Oracle database. When querying an elaborate view, queries take a while. I would like to place and animated wait dialog up...
8
by: Luc | last post by:
Hi, I am writing software to automate some testing. I have one main form to set up the tests, and once this is complete, I open 4 identical forms to monitor each different device that I am...
8
by: WhiteWizard | last post by:
I guess it's my turn to ASK a question ;) Briefly my problem: I am developing a Windows app that has several User Controls. On one of these controls, I am copying/processing some rather large...
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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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...
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
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
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.