NextCard Internet Visa - Apply Now

PC Magazine

  PC Tech

What's in a Thread?

Introduction

Threads: The Atomic Unit of Execution

Thread Scheduling

Uses for Multiple Threads

Hands On: Single vs. Multiple Threads

Further Reading



X10.com - The SuperSite for Home Automation!

NextCard Internet Visa - Apply Now

 
  Categories:
Operating Systems
What's in a Thread?
Thread Scheduling

Continued from Threads: The Atomic Unit of Execution

For the operating system, deciding which thread to execute next and how long to let it run is a complicated process involving many factors. It's not just a matter of setting a timer to fire every few milliseconds and activating each thread in turn. Such a system would offer little improvement over the 16-bit systems we're used to today.

The primary consideration in scheduling threads for execution is each thread's execution priority. Windows 95 and Windows NT assign each thread a priority value from 0 to 31, with higher numbers indicating higher priority. Priority 31 is reserved for extremely critical tasks such as real-time data acquisition, while priority 0 is used by the operating system for certain idle-time tasks that are performed only when nothing else is going on. Most threads run at priority levels from 7 to 11. Every few milliseconds, the operating system's scheduler evaluates all the threads in the system and allocates processor time to the one with the highest priority. If two or more threads have equally high priority, the one that has been executed least recently is executed next. In general, high-priority threads are always executed before low-priority threads, and threads with low priority never preempt threads with high priority.

Does this mean that if a priority-10 thread is executing in the system along with four priority-9 threads, the priority-9 threads will never execute? Not at all. Remember, in Windows everything is message-based. A Windows application doesn't process mouse and keyboard input directly; rather, it receives messages from the operating system informing it that a mouse or keyboard event has occurred. If the priority-10 thread empties its message queue--that is, if there is nothing more for it to do until another message comes along--it is suspended so that lower-priority threads can execute. A suspended thread receives no processor time until something wakes it up. When the sleeping priority-10 thread receives a message, it will wake up and preempt a priority-9 thread that is currently executing. Until then, it consumes zero processor time. Most threads spend a lot of time in a suspended state waiting for input events, so in all but the most extreme cases even priority-0 threads get ample time to execute. Nonetheless, a poorly written application can still bring the rest of the system to a crawl. Fortunately, poor threading design is very obvious in testing, and most software makers iron out the performance problems before shipping.

Windows 95 plays a lot of tricks with priority levels to make applications as responsive as possible. For instance, Windows 95 automatically boosts the priorities of the active application's threads by 1 so it will receive priority over applications running in the background. This makes the foreground application quicker to respond to user input. In addition, the scheduler temporarily boosts the priority level of a thread that receives an input message by 1 or 2 so the processing of that message will receive priority over just about everything else. The thread is then gradually reduced to its normal priority level so it won't consume processor resources needlessly.

Continues...

Published as Tutor in the 12/05/95 issue of PC Magazine.


 SPONSORED LINKS
@Backup   Your Solid Online Backup Plan. Download Now.
Services   9c/MINUTE LONG DISTANCE, 5c/MINUTE ON SUNDAYS!
STORAGE   Quantum means non-stop business, 24 hours a day
Software   X10.com -- The SuperSite for Home Automation
Books   Bargain Books up to 90% off at barnesandnoble.com
 ZDNET FEATURED LINKS
Downloads   Check out the best new downloads in Reviewer's Raves
Bargains!   Shop the Basement for best buys on computer products
Free Help   Got computing questions? ZDHelp has all the answers!
 MAGAZINE OFFERS
Free Offer   Get a FREE SUBSCRIPTION to Inter@ctive Week

TOP
Copyright (c) 1997 Ziff-Davis Inc.