OS Threads behavior

G

Guest

I would like to know how threads behavior in .NET .
When an application create 4 threads for example start all of them, the OS
task manager will execute all 4 thread in deterministic order manes,
OS execute (All have same priority)
Thread#1
may be other threads,
Thread#2
may be other threads,
Thread#3
may be other threads,
Thread#4
may be other threads,
Thread#1
may be other threads,
Thread#2
may be other threads,
Thread#3
may be other threads,
Thread#4
.......
I'm right ? if yes , is there any way to ask for run all those threads
simultaneity with no order ? or I will have to managed the tasks by suspend /
resume for example ?
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi,

I don't think you are right, and even more I don't think that you should
care about it, it's the OS the one who decide which thread to execute. Like
for example, what happens when executed on a dual core processor, or a
processor with HT, the OS can execute two threads at the same time under
this escenario.

What r u trying to do?


cheers,
 
G

Guest

I care about it, since I need it, "This answer is so famelies in Microsoft
responds , and I don't think it should be the way :-( ".
I need it since I trying to built an application which manage concurrency
tests, those test will access an external hardware connected to PC by LPC.
I care since I would like to be sure the tests will run in 100% simultaneity
to be able to verify the HW modules are acting as will as expected. Each test
run in deferent thread and if the threads are running in order this will not
simulate all the validation cases may cause HW to fail and we will not able
to catch all BUGS.
BYE the way I was working so hard in my company trying to integrate C# and
..Net to our testing engineering , and I already made more than 4 great and
heavy tools in this language, the language does not support Hardware access
and (Semiconductors boundary )as C/C++, this is the reason I merge both
languages for my tools, All high level and user interface made by C# (As
manager tool) and the low level (like HW access /IO/MEM?USB....) by C/C++.
Thanks for your respond .
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi,


I care about it, since I need it, "This answer is so famelies in Microsoft
responds , and I don't think it should be the way :-( ".

First of all, it's my opinion, I had no relation at all with MSFT,

What thread will be executed next, or in what order is IMO impossible to
predict, it does depend of the scheduler :
http://msdn.microsoft.com/library/d...en-us/cpguide/html/cpconSchedulingThreads.asp

IT does depend of the OS , for example you cannot assure the same results in
XP/2K/2K3 or even in XP home/pro.

I need it since I trying to built an application which manage concurrency
tests, those test will access an external hardware connected to PC by LPC.
I care since I would like to be sure the tests will run in 100%
simultaneity
to be able to verify the HW modules are acting as will as expected. Each
test
run in deferent thread and if the threads are running in order this will
not
simulate all the validation cases may cause HW to fail and we will not
able
to catch all BUGS.

The threads will NOT run at the same time, no matter what you do , or what
environment you use. only one thread will be running at a given moment per
processor or core.




cheers,
 
G

Guest

Yes I know since we have one CPU,
But my question was Is the threads schedule randomly or there is an
order,the one create first run first the the seconed ....

"how to schedule threads randomly on Windows OS"
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi Yosi,

I advise you to read a little more about how an OS schedule jobs, ITOH you
contradice yourself, you want to run ALL the thread concurrently but also
randomly ?

I think it's safe to consider that the threads will be run randomly without
doing nothing else, if you want to include a little more randomness you
could play with ThreadPriority when you create your threads.

Othen than that, there is nothing much else you can do

cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
 
W

Willy Denoyette [MVP]

Unless you have a quad CPU machine (or a dual HT box), there is no chance to
run 4 threads simultaneously( that is exactly at the same time). Even if you
have 4 CPU's there is NO guarantee YOUR 4 threads will ever run in parallel,
there are other threads in the system that are scheduled as well and these
might run at higher priorities than your threads. For instance, one of these
threads is your UI thread which runs with a higher priority than your
auxiliary threads, just move your mouse and it will get a CPU.

Willy.
 
G

Guest

I know everything your write, maybe my question was not clear enough, so here
is my question again :
When I make more than one thread , Does OS run them in order one by one
means x commands from thread1 x commands from thread2... or the OS run them
simultaneity (with no order)?
From my old knowledge if I have xx thread/tasks with same priority OS run
them by order the task has been created firs run firs the second run after
and so ....
But I trying to be sure is this the same in XP/2K OS (.Net) , or not ?
If this is the same , is there anything to do with this order except to play
with the thread suspend/resume or abort/run deferent order each time?
 
W

Willy Denoyette [MVP]

The scheduler tries to schedule threads in all fairness, that is it will try
to give each thread in the system a fair chance to run even in the presence
of higher priority threads.
The result of this might be that when your threads (the 4 threads) are
executing the exact same code at the same priority that they might always be
scheduled in the same order.
Say you have started the threads in order 1, 2, 3 and 4 with the same
priority.
The will be scheduled as 1...2...3...4...1...2...3...4 (this supposes none
of them get blocked waiting for an external event).
Note ... is the time other threads in the system are being scheduled.
If you change the priority of one of the threads you might break this
relative order, because higher privileged threads are scheduled more often
than the lower priority ones, but the order in which they are scheduled will
again be the same.
Say you set above thread 2 priority to above normal.

You might see something like:
1...2...3...2...4...2...1...2...3...4...2...1...2...3...2...4...2

You see 2 gets more cpu time but the order of the others remains the same,
but again all depends on what the threads are doing.


Willy.
 
W

Willy Denoyette [MVP]

If you need to run your threads in a kind of 'random' order, you need to
'schedule' your threads yourself, but don't do this using Thread.Suspend ...
Resume, these API's are broken and are removed from the next version of the
framework.
Instead you should use synchronization event primitives like
ManualResetEvent to coordinate logical tasks. Note that I'm not saying
threads, as you can't effectively schedule threads from user code, but at
least you can select a thread or a number of threads to be scheduled while
you keep others are waiting for an event to arive.


Willy.
 
S

S. Senthil Kumar

I guess what the other posters are saying is that you cannot depend on
the order. It is dependent on a lot of factors like what each thread is
doing, the number of processors, the operating system etc.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top