OSRLogo
OSRLogoOSRLogoOSRLogo x
OSRLogo
x

Everything Windows Driver Development

x
x
x
GoToHomePage xLoginx
 
 

    Thu, 02 Sep 2010     81215 members

   Login
   Join


 
 
Contents
  About This Site
What's New?
Hector's Memos
The NT Insider
The Basics
File Systems
Downloads
ListServer / Forum
Driver Jobs
Store
  Express Links
  · It's Here: The NT Insider -- Digital Edition!
  · WDK Community Bug Bash 2010 -- Submit a Bug... Get FREE STUFF!
  · File Systems and Filters: A Specialty
  · It's All About The Basics
  · The NT Insider - Digital Edition

What is a Thread?

The Windows Operating System has a number of key architectural concepts.  One of these is the concept of a thread because it forms a fundamental keystone of many of the tasks performed by the OS.

In Windows, a thread is a unit of execution.  This unit of execution is defined by the state of the processor at a given instant in time.  In a nutshell, it is all of the state information that is necessary to place a CPU in the same state.  Thus, it includes the values stored within the processor registers as well as memory presently used by the processor for state management.  Of course, the details of "processor state" really are machine dependent.

For example, with the x86 (IA-32) processor, the state of the processor includes the values stored in the general purpose registers (EAX, ECX, EDX, EBX,) the index registers (ESI and EDI,) the stack registers (ESP, EBP), the instruction pointer (EIP), segment registers (CS, DS, SS, ES, FS, and GS,) and machine state registers (EFLAGS, CRx registers).

If Windows decides to cease processing a particular thread and begin processing a different thread, it does so by storing the current processor state and then retrieving the processor state of the other thread.

One of the keys to this context is the stack.   This is because certain instructions rely upon the contents of the stack to process instructions.  For example the ret instruction on the x86 replaces the value in EIP with the value on the top of the stack.  Thus, when Windows changes the stack pointer (ESP) as part of switching threads and then executes a ret instruction, this results in the processor using the value on the stack as the instruction to execute next.  Thus, switching between threads can be done with the same code, but different stacks.

Threading in Windows is a fundamental part of the OS.  The low level part of the operating system (the micro-kernel) implements thread switching.  This, in turn, allows Windows to process different programs by switching between them as necessary.  If the computer has multiple processors, each processor is identical - at least in terms of its execution context - and thus the thread may be executed first on one processor and later on a different processor.  Note, however, that one thread may only be executing on one processor at one instant in time.  Yes, it could change processors from one instant to the next, but not on two processors at one instant.

Kernel drivers often must handle threading issues, because even though there are different execution contexts (register state and stacks) they may utilize the same code paths.  In other words, a single driver might be operating on behalf of multiple different threads at the same time.  For new kernel mode developers this might be very different than the environments to which they have become accustomed.  The advantage in the kernel environment is that it allows the OS to process many different requests, intermixing them as useful in order to optimize the overall performance of the system.

Thus, a thread is nothing more than the unit of execution used by Windows to perform tasks (or work) and provide useful services.

User Comments
Rate this article and give us feedback. Do you find anything missing? Share your opinion with the community!
Post Your Comment

"Nice explanation"
Particularly liked thread/context switching explanation ...but is there any other mechanism that windows uses to accomplish this (e.g. using jmp or call instruction)?

Rating:
23-Dec-08, Piyush Baid


"Threading this explanation"
Just executing some of my brains threading. begin thread Gr8 explanation ret end of thread

Rating:
16-Apr-04, Tonny Poulsen


Post Your Comments.
Print this article.
Email this article.

Writing WDM Drivers LAB
Seattle, WA
16-Aug-2010 to 20-Aug-2010

Writing WDF Drivers LAB
Santa Clara, CA
27-Sept-2010 to 1-Oct-2010

Kernel Debugging &
Crash Analysis LAB

Portland, OR
18-Oct-2010 to 22-Oct-2010

Developing File Systems
Santa Clara, CA
26-Oct-2010 to 29-Oct-2010

Windows Internals &
Software Drivers LAB

Santa Clara, CA
15-Nov-2010 to 19-Nov-2010

 
 

Windows Debugger
V6.12.2.633 -- 26 Feb 10

Checked Build Downloads
29-Apr-10

Debugging Symbols
5-Oct-09
 

WDK Doc Updates
Now updated bi-monthly!

Windows WDK
V7.1.0 -- 26 Feb 10

 
 
x
LetUsHelp
 

Need to develop a Windows file system solution?

We've got a kit for that.

Need Windows internals or kernel driver expertise?

Bring us your most challenging project - we can help!

System hangs/crashes?

We've got a special diagnostic team that's standing by.

Visit the OSR Corporate Web site for more information about how OSR can help!

 
bottom nav links