1 #include
2 #include
3
4 static int number=10;
5 CRITICAL_SECTION CriticalSection;
6
7 DWORD WINAPI ThreadOne(LPVOID lpParameter)
8 {
9 printf("窗口1售票开始:\n");
10 while(1)
11 {
12 EnterCriticalSection(&CriticalSection);
13 if(number>0)
14 {
15 printf("窗口1售出第%d张票...\n",number);
16 number--;
17 Sleep(1000);
18 }
19 LeaveCriticalSection(&CriticalSection);
20 Sleep(100);
21 }
22 return 0;
23 }
24 DWORD WINAPI ThreadTwo(LPVOID lpParameter)
25 {
26 printf("窗口2售票开始:\n");
27 while(1)
28 {
29 EnterCriticalSection(&CriticalSection);
30 if(number>0)
31 {
32 printf("窗口2售出第%d张票...\n",number);
33 Sleep(1000);
34 number--;
35 }
36 LeaveCriticalSection(&CriticalSection);
37 Sleep(100);
38 }
39 return 0;
40 }
41
42
43 int main()
44 {
45 HANDLE HOne,HTwo;
46 InitializeCriticalSection(&CriticalSection);
47 printf("***********************vpoet******************\n");
48 HOne=CreateThread(NULL,0,ThreadOne,NULL,0,NULL);
49 HTwo=CreateThread(NULL,0,ThreadTwo,NULL,0,NULL);
50 CloseHandle(HOne);
51 CloseHandle(HTwo);
52 while(TRUE)
53 {
54 if(number==0)
55 {
56 printf("不好意思,票卖完了!\n");
57 DeleteCriticalSection(&CriticalSection);
58 return 0;
59 }
60 else
61 {
62 continue;
63 }
64 }
65
66 return 0;
67 }