7.1. Understanding the Problem

If you have never worked in a preemptive multi-threaded environment before, it may be hard to imagine that anything peculiar can happen. If you have worked in such an environment, then you know the problems all too well. Let's look at two very simple examples of the problems that can arise when you use multiple threads in Windows.

Listing 7.1 contains a simple program. Upon starting, the program immediately creates four identical threads. Each thread increments the same global variable 250,000 times, so count is incremented a total of 1,000,000 times. The main program waits for the four threads to complete, and then prints out the global count variable. (See Chapter 6 for an explanation of thread functions and of ...

Get Win32 System Services: The Heart of Windows® 98 and Windows® 2000 now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.