6.3. Simple Examples

For many people, the whole idea of creating multiple threads within a single process requires some mental adjustment. Let's start by looking at several extremely simple examples to see how threads work in Windows.

Listing 6.1 contains a very simple program. In this program, the code prints the value of the global variable named count each time the user presses the return key. Nothing in the program changes count, so the program always prints zero. There is no magic here.

Now let's add a thread to Listing 6.1, as shown in Listing 6.2 and Figure 6.3. A thread in Windows is simply a function that executes in the background. The function CountThread in Listing 6.2 increments the global variable count and then sleeps for 100 ...

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

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.