[Home]
[Edit this page]
[Recent Changes]
[Special Pages]
[Help]
BeginnersGuideToThreads
Beginner's Guide To...
Technically - A thread is a collection of the state of a CPU at any given time. Such state information includes the processor's registers and stacks. Each thread has two stacks - a kernel-mode stack and a user-mode stack. Each thread in the system executes for a short period of time, after which the system saves the state information for this thread into a data structure. To execute another thread, the system loads the information from this data structure onto the CPU. The system then allows the CPU to exceute the current instruction pointed to by the IP register.
The extra dimension that multithreaded applications and hardware added was to provide a means of running more than one Thread (program) at a time.
Any program that you write is run as a thread of the Operating system (Windows, Linux, et al).
But generally when people talk of threads, they are discussing threads in an application, not in the OS.
What is a thread good for?
Well doing more than one thing at a time is the simple answer, Windows itself has many threads running, each one performing a different task.
In your application you can use it to...
What happens when a thread executes?
At some point in the program a decision is made to execute a thread. At that point, a special function (CreateThread) is then called.
Information about what you want to run is given to that function via its parameters, the function then prepares the CPU to run the code and returns.
When the cpu is ready it switches to the code and it runs, when the code is finished the thread simply ends.
While all this is going on, your program can continue almost unabated.
The astute amongst you may have realised that there is no way to pass back to the main program the results of running the code, because it simply ends, and who knows what your program is doing at the time it ends.
And this is where the real fun starts....
Return to threads
[Edit this page] [Page history] [What links here] [Discuss this topic] [Printer Friendly]
BeginnersGuideToThreads
Beginner's Guide To...
Threads
A thread is simply a program that can be run on a computer. Before the advent of "multi threaded" applications, computers could only run one program at a time. Examples are DOS and even Windows 3.0+.Technically - A thread is a collection of the state of a CPU at any given time. Such state information includes the processor's registers and stacks. Each thread has two stacks - a kernel-mode stack and a user-mode stack. Each thread in the system executes for a short period of time, after which the system saves the state information for this thread into a data structure. To execute another thread, the system loads the information from this data structure onto the CPU. The system then allows the CPU to exceute the current instruction pointed to by the IP register.
The extra dimension that multithreaded applications and hardware added was to provide a means of running more than one Thread (program) at a time.
Any program that you write is run as a thread of the Operating system (Windows, Linux, et al).
But generally when people talk of threads, they are discussing threads in an application, not in the OS.
What is a thread good for?
Well doing more than one thing at a time is the simple answer, Windows itself has many threads running, each one performing a different task.
In your application you can use it to...
- Speed up your program (often just in perception, but this is VERY important)
- Run background functions such as a spell checker in a word processing app.
- Implement a "hurry up" function in a video game
- Seperate tasks into logical units that can be run concurrently
What happens when a thread executes?
At some point in the program a decision is made to execute a thread. At that point, a special function (CreateThread) is then called.
Information about what you want to run is given to that function via its parameters, the function then prepares the CPU to run the code and returns.
When the cpu is ready it switches to the code and it runs, when the code is finished the thread simply ends.
While all this is going on, your program can continue almost unabated.
The astute amongst you may have realised that there is no way to pass back to the main program the results of running the code, because it simply ends, and who knows what your program is doing at the time it ends.
And this is where the real fun starts....
Return to threads
[Edit this page] [Page history] [What links here] [Discuss this topic] [Printer Friendly]
