views:

64

answers:

2

Reading CLR via C# 2.0 (I dont have 3.0 with me at the moment)

Is this still the case:

If there is only one CPU in a computer, only one thread can run at any one time. Windows has to keep track of the thread objects, and every so often, Windows has to decide which thread to schedule next to go to the CPU. This is additional code that has to execute once every 20 milliseconds or so. When Windows makes a CPU stop executing one thread's code and start executing another thread's code, we call this a context switch. A context switch is fairly expensive because the operating system has to:

So circa CLR via C# 2.0 lets say we are on Pentium 4 2.4ghz 1 core non-HT, XP. Every 20 milliseconds? Where a CLR thread or Java thread is mapped to an OS thread only a maximum of 50 threads per second may get a chance to to run?

I've read that context switching is very fast in mircoseconds here on SO, but how often roughly (magnitude style guesses) will say a modest 5 year old server Windows 2003 Pentium Xeon single core give the OS the opportunity to context switch? 20ms in the right area?

I dont need exact figures I just want to be sure that's in the right area, seems rather long to me.

A: 

Your "50 threads at a time" math is wrong. You assume that each of those threads is in a 100% CPU state. Most threads are in fact asleep waiting for IO or other events. Even then, most threads don't use their entire 20 ms before going into IO mode or otherwise giving up its slice.

Try this. Write an app with an inifinite loop (eats its entire CPU window). Run 50 instances of it. See how Windows reacts.

Frank Krueger
43 threads get to see some CPU when 100%. Cant get enough threads to get a result when they give up share after a few ms (sleep) but 1000+
mattcodes
CPu 100%. Without debugger 125 cpu bound threads seen, suggesting the 20millisecond is out dated in Windows7. The actually exe obviously context switches thousands of times between the half a dozen .net internal threads
mattcodes
I thought context switch these days was 4ms...
Blank Xavier
A: 

I just did a test I got 43 threads seeing its share in a second (after warming up) which makes Richter statement pretty accurate (with overhead) I say. Quadcore/Win7/64bit. Yes these were 100% cpu threads so obviously they weren't given themselves back before their 20ms. Interesting

mattcodes