Partager via


Threading deep dive – Day 2

Each and every thread that executes your code statements will undergo whatever we discussed in day1. Your program should have at least one thread to be alive. Whenever the process is created by the OS, it creates the process with one thread which is called primary thread. Your program can have more than one thread. Windows operating system does not switch processes. It switches threads. If your program contains more than one thread, then each thread get some time slot from the processor. Windows kernel controls how long a thread can take processor time.

    With win 3.1 and earlier, operating systems from Microsoft followed co-operative multithreading, where the executing thread [the thread that is executing something in the processor] should yield processor time to the other waiting thread. It is each thread's responsibility to yield processor time to other thread with good co-operation. Each thread from co-operative multi-threading model is expected to be good citizen by giving processor time to other threads. With this model, an application written inappropriately can bring the OS down and non-responsive.

From Windows 95 onwards, all the Operating systems from Microsoft, followed Preemptive multi-threading model where Kernel has control on when to preempt the executing thread and switch to another thread. Kernel considers thread priority during thread switching. But with managed code, the host decides whether to honor managed thread priority or not. It means even if managed thread priority is set, the host may not set the mapped native thread priority. We will look at the relation between managed threads and native threads later.

    Quantum is the length of time the thread can possibly execute on a processor before the OS interrupts to schedule other threads. A thread does not necessarily always completes it quantum time as windows uses preemptive multi tasking. Quantum can vary from one thread to another thread. Quantum varies between different versions windows also, like windows client OS keeps different value for the thread quantum than their server counterparts. OS users can influence the change in quantum value by doing the following.

  1. Right click on the my computer from your desktop
  2. Click Properties
  3. Click on Advanced Tab
  4. Click the settings button from the "Performance" group
  5. There would be following options available in the resulting window.
    1. "Let windows choose what's best for my computer"
    2. "Adjust for best appearance"
    3. "Adjust for best performance"

Choosing option 2 would let the windows set smaller value for the quantum so as to have a best user experience. Choosing option 3 would let the OS to set bigger value for the quantum in order to let the background services threads run for a longer duration with less context switches for quick response from the service. For further read on Thread quantum, scheduling, priority boosting etc, I would suggest "Microsoft Windows internal, Fourth Edition"  Winternals  from MS Press by Mark E. Russinovich and David A. Solomon

Comments