views:

198

answers:

5

I created a simple .NET console application and within it I start 3 managed threads. Each thread executes the following loop:

        while (true)
        {
            System.Console.WriteLine(thread.Name + " " + DateTime.Now);
            Thread.Sleep(10);
        }

I set the first thread's priority to high and let the app run for several minutes. Overall I expected the first thread to write more frequently to the console. In fact, it had no noticeable effect. Setting the priority to high for one thread seemed the same as leaving all three set to normal.

What about thread priorities am I missing here that would explain this?

+5  A: 

Try Sleep(1) instead. 10ms is eternity in thread time, however Sleep(1) basically says "I yield the rest of my time slice for execution to another thread" which I think is what you want. Thread priority doesn't just make a thread execute more often, it just says "if the scheduler has a choice between giving thread A a timeslice and thread B a timeslice, give it to the one with the higher priority." Try this change and see what happens. Alternately, make each thread take a different amount of time to execute (don't use sleep for this though, print 100 elements in a loop on one, print 1000 on another, etc.)

Edit: changed to Sleep(1), not sure why it was modified. See my comment below for why Sleep(1) is a better choice here.

Jeff Tucker
To "yield the rest of my time slice for execution to another thread" I'd suggest Sleep(0) instead of Sleep(1).
ChrisW
This is a common misconception but Sleep(1) is what you want. Sleep(0) yields execution only to threads of the same priority. Sleep(1) will yield to any thread that is available for execution. Here's a good blog on the subject: http://www.bluebytesoftware.com/blog/PermaLink,guid,1c013d42-c983-4102-9233-ca54b8f3d1a1.aspxI thought Sleep(0) was correct for many years but I was wrong.
Jeff Tucker
Thank you. (padded to 15 characters)
ChrisW
A: 

The threads won't write more frequently to the console because they're all waiting 10ms between writes. However, the high-priority thread should write to the console more regularly, i.e. closer to true 10ms between writes. This is because, while the lower-priority threads can get pushed aside by other tasks on the PC, the higher-priority thread will have to wait less before being allowed to write.

Does that answer your question?

rwmnau
+1  A: 

If you lose the Thread.Sleep statements you are likely to see more action from the higher-priority thread. Thread.Sleep gives the other thread(s) plenty of time to do their thing.

Fredrik Mörk
Interesting to note is that when I took out the call to Sleep altogether, Windows did a poor job at managing priority. The rough % of CPU time for the threads were 28, 43, and 27, with the high priority thread getting the least time. Strange.
More random behavior occured when I started 10 threads of normal priority and without calls to Sleep(). There was an even/odd thread phenomenon where all even threads got double the time of the odd threads. Run it again and now the odd threads got double the time. Seemed to toggle this way again and again.Putting in calls to Sleep() allow all threads to get equal time. It seems calls to Sleep() are required to "harmonize" threads with one another, an unfair burden for Windows to place on applications.
+3  A: 

If printing by all three threads takes less than 10ms then you won't see any effect on output. Try to come up with a task that takes a long time say calculate square roots up to a million then see who runs more often.

stonemetal
+1 - he'll get a better picture by doing something which actually requires nontrivial cycles
DarkSquid
Thanks.The Thread.Sleep(10) did seem to be the key in that it allows other threads to work. Reducing the time to 1ms gave me the results I was expecting. Also, when the high priority thread was given more work to do, it did seem to get even more CPU time.
+1  A: 

ThreadPriority may not behave as expected... some reading material on thread priority...

Thread Priorities are Evil
http://www.codinghorror.com/blog/archives/000671.html

ThreadPriority.BelowNormal
http://odetocode.com/Blogs/scott/archive/2006/08/27/6053.aspx

Why Sleep(1) is better than Sleep(0)
http://www.bluebytesoftware.com/blog/PermaLink,guid,1c013d42-c983-4102-9233-ca54b8f3d1a1.aspx

Axl