What Is Multithreading?

Multithreading allows your application to perform several tasks at the same time. In fact, "at the same time" isn't truly accurate. Unless your computer is equipped with multiple CPUs, it can execute only one instruction at a time. (And even if you have multiple CPUs, your computer will almost always have far more threads in existence than it has processors.)

The truth of the matter is that when you use threads, you program as though these tasks were running independently. That means if you have two threads performing distinct tasks, you can't make any assumption about which thread will finish first.

Behind the scenes, the Windows operating system controls the life of all threads, acting a little like an administrator controlling access to a network supercomputer. Windows gives each thread a brief unit of time (called a time slice) to perform some work with the CPU, and then it freezes the thread in a state of suspended animation. A little bit later (perhaps only a few microseconds), the system unfreezes the application thread and allows it to perform a little more work. This invasive form of constant interruption is known as preemptive multitasking.

The most important thing to understand about threading is that it's all a matter of perception. For the most part, each thread behaves as though it is running exclusively and has complete control of the CPU. The controlling program acts as though the threads are running simultaneously. The user carries on as though the program can perform several tasks at once, perhaps even initiating multiple concurrent tasks, even though these tasks might complete more slowly when performed at the same time. Note that if you have more than one CPU, Windows might choose to execute different threads by using different CPUs, but this isn't necessary.

In a multithreaded program, each independent unit of execution is a thread. A complex application (or process) can have dozens of threads executing simultaneously. By using Task Manager in Windows, you can see all the executing processes, but you can't tell how many threads are in progress.



Microsoft. NET Distributed Applications(c) Integrating XML Web Services and. NET Remoting
MicrosoftВ® .NET Distributed Applications: Integrating XML Web Services and .NET Remoting (Pro-Developer)
ISBN: 0735619336
EAN: 2147483647
Year: 2005
Pages: 174

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net