Chapter 5: Concurrency

In a simple program, one user interacts with the system, which does one thing at a time. You push a button and the system reacts. You wait for that reaction to end and then you take another action.

In a modern system, running on Windows 95, NT or another multi-tasking operating system, you no longer have the luxury of assuming that only one thing will happen at a time. Windows is quite content to manage multiple tasks "simultaneously". Simultaneously is in quotes because the operating system provides the illusion of simultaneity, rather than the reality. It does so by time-slicing.

Time slicing is where the operating system divides a second into many parts and works on each task for a fraction of a second; switching between them in sequence. Since these tasks switch quite quickly, it appears to the user that they are happening at the same time.

Some computers have more than one CPU, and Windows NT is capable of using each CPU for a different task. In this case, the illusion gives way to reality. With more than one CPU it is possible to achieve true simultaneity.

The interesting thing, though, is that nothing should change except performance. In a well-designed system it doesn't matter to your code at all whether it is running in a multi-CPU environment, or just in a time-sliced single-CPU environment.

This chapter introduces threads and processes, the elements essential in creating applications that support multi-tasking. It also explains the benefits you will derive from creating a multi-threaded application. We will also look at the difficulties threads create, and the techniques you can employ to overcome these problems.

© 1998 by Wrox Press. All rights reserved.