Starting a Win32 thread using C can be done using the Win32 CreateThread function. This is a Windows system call that will create a thread in the current process. Threads execute concurrently, either in OS-managed time slices or on separate processors. The CreateThread function requires a function that the thread will run. The function is passed as a function pointer. The CreateThread function returns a handle to the thread which can be used to get information about the thread and control its behavior. The following example code demonstrates using threads in a Win32 program:
#include <Windows.h> #include <stdio.h> #include <time.h>
DWORD WINAPI BobGrillSteak (LPVOID lpParam) { printf(“Bob will grill the steak, it will take 10 minutes.\n”); Sleep(15000); /* In this universe, seconds are minutes */ printf(“Bob has finished grilling the steaks.\n”); return 0; }
DWORD WINAPI AliceBakeCake (LPVOID lpParam) { printf(“Alice will bake the cake, it will take 30 minutes.\n”); Sleep(30000); /* In this universe, seconds are minutes */ printf(“Alice has finished baking the cake.\n”); return 0; }
DWORD WINAPI FrankSaladShake (LPVOID lpParam) { printf(“Frank will toss the salad, it will take 1 minute.\n”); Sleep(1000); /* In this universe, seconds are minutes */ printf(“Frank has finished tossing the salad.\n”); return 0; }
int main (void) { HANDLE workers[3]; /* Bob, Alice and Frank */ time_t tstart, tend;
/* Note the time when work began */ tstart = time(NULL); /* In this universe, seconds are minutes */
/* Start the work */ workers[0] = CreateThread(NULL, 0, BobGrillSteak, NULL, 0, NULL); workers[1] = CreateThread(NULL, 0, AliceBakeCake, NULL, 0, NULL); workers[2] = CreateThread(NULL, 0, FrankSaladShake, NULL, 0, NULL);
/* Wait for the work to complete */ WaitForMultipleObjects(3, workers, TRUE, INFINITE);
/* Note the end time, and report how long it took */ tend = time(NULL); /* In this universe, seconds are minutes */ printf(“All of the work was completed in %d minutes.\n”, tend – tstart);
fgetc(stdin); return 0; } |
The code first includes ‘Windows.h’ which will define the CreateThread function. After the includes, three thread functions are defined. These functions will all run concurrently in separate threads. The main function first declares three handles, one for each thread, then defines a start and end time that will be used to time the threads. The start time is recorded before the threads are started, and the end time is recorded after all the threads have finished. Next, the three threads are created with three calls to CreateThread. Each call receives one of the thread functions as its argument. After the threads are created, the function WaitForMultipleObjects is used to wait for all three threads to exit. When a thread exits, its handle is signaled. After all of the three handles are signaled, WaitForMultipleObjects will return. The expected output from this program is:
Alice will bake the cake, it will take 30 minutes. Bob will grill the steak, it will take 15 minutes. Frank will toss the salad, it will take 1 minute. Frank has finished tossing the salad. Bob has finished grilling the steaks. Alice has finished baking the cake. All of the work was completed in 30 minutes. |
Note that the total time taken is the same as the longest time taken by one of the threads. This is expected for threads that run concurrently.