37 h = CreateThread(NULL, 0, thread_entry_point, NULL, 0, NULL);
38 if(h == NULL)
39 {
40 printf("CreateThread() failed.\r\n");
41 return(1);
42 }
43
44
while(1)
45 {
46 EnterCriticalSection(&lock);
47
48
++g_val;
49 printf("thread 1 , g_val: %d\n", g_val);
50
51
LeaveCriticalSection(&lock);
52
53
Sleep(1000);
54 }
55
56
DeleteCriticalSection(&lock);
57
58
return(0);
59 }
Example Execution
Let’s look at the thread4.c output.
Output
C:\Documents and Settings\Mike\
My Documents\Visual Studio Projects\thread4\Debug>thread4.exe
thread 1 , g_val: 1
thread 2 , g_val: 2
thread 1 , g_val: 3
thread 2 , g_val: 4
Analysis
■
At line 7, the windows.h header file is included.This header file is required for
the CreateThread and CriticalSection family of functions.
■
At line 11, the lock variable is ...