views:

39

answers:

1

I have soft real-time .NET application run on W2008R2.

I just realised that I cant explain how precisely threads are being scheduled.

And for my embarassement I dont know how it works down to OS threads at all.

So I will explain what I know and I'd appreciate if anyone could help me to fill the gaps and refer me to a simple description of algorithms which are used in .NET and Windows to schedule threads.

My code runs in managed threads. As I know managed threads (lets call them .NET threads) run in unmanaged threads (lets call them OS threads).

I know that threads are competing for CPU time and other resources. And that there is a piece of software - scheduler which monitors resources and threads and make the whole thing work.

Here I am not sure - is the scheduler just one for the OS or there is also .NET scheduler which schedules .NET threads? And if there are two schedulers then how they are working together?

And what are the rules to schedule a thread?

I want to narrow this question to a thread which does only arithmetic computations and no access to any devices so we are talking purely CPU consuming thread.

How scheduler factors .NET thread priority, process priority and a shared resource (CPU in this case) ?

+1  A: 

The current versions of the .NET desktop runtime do have a 1:1 mapping between .NET threads and OS threads. This may change in future versions (and may not be true in other hosts; I know it's not true in AutoCAD but I believe it's true in SQL Server and MS Office).

Note that the .NET thread pool is different than the OS thread pool (though it does make use of a single process-wide I/O Completion Port, which has some pool-like semantics).

For full and complete answers, you'll need two books: Windows Internals and Concurrent Programming on Windows. I recommend reading them in that order.

Stephen Cleary
Stephen I am very fond of guys like Russinovich and Duffy but to read 2 thick books only to understand how threads are scheduled.... I am sure scheduler uses very simple rules. I.e. if a thread has priotiry X and it signals READY and there is available CPU core and there are no READY threads with priority > X then run this thread....
Bobb
Additionally, .NET 4.0 provides an improved ThreadPool that allows the pool to have it's own (possibly user defined) Task Scheduler. However, I believe that this only applies to starting new threads, not managing running ones (I could be wrong on that though) and once a thread is running it's running under the OS's thread scheduler.
Mystere Man
@Bobb: Actually, thread scheduling is quite complex. I recommend Windows Internals for the overview (e.g., "hard" and "soft" thread priorities), and CPoW for the details.
Stephen Cleary
@Mystere: Are you thinking of the `TaskScheduler` class? If so, it handles scheduling of the `Task` objects, not threads. The most commonly-used `TaskScheduler` just uses the thread pool (with work-stealing queues and all that complex stuff).
Stephen Cleary
you might be right... reading now...
Bobb