windows系统调用 临界区机制
2020-12-13 03:00
标签:style class blog code color get windows系统调用 临界区机制,搜素材,soscw.com windows系统调用 临界区机制 标签:style class blog code color get 原文地址:http://www.cnblogs.com/593213556wuyubao/p/3791566.html 1 #include "iostream"
2 #include "windows.h"
3 #include "cstring"
4 using namespace std;
5
6
7 static int g_nIndex=0;
8 const int MAX_TIMES=10;
9 static DWORD g_dwTimes;
10 CRITICAL_SECTION g_CriticalSection;
11
12 DWORD WINAPI IncProc(LPVOID lpParam){
13 BOOL fDone=FALSE;
14 while(!fDone){
15 EnterCriticalSection(&g_CriticalSection);
16 if(g_nIndex>=MAX_TIMES){
17 fDone=TRUE;
18
19 }
20 else{
21 g_dwTimes++;
22 printf("The Inc count‘s value is%d.\n",g_dwTimes);
23 g_nIndex++;
24 Sleep(10);
25 }
26 LeaveCriticalSection(&g_CriticalSection);
27 }
28
29 return(0);
30 }
31
32 DWORD WINAPI DecProc(LPVOID lpParam){
33 BOOL fDone=FALSE;
34 while(!fDone){
35 EnterCriticalSection(&g_CriticalSection);
36 if(g_nIndex>=MAX_TIMES){
37 fDone=TRUE;
38 }
39 else{
40 g_dwTimes--;
41 printf("The Dec count‘s value id %d.\n",g_dwTimes);
42 g_nIndex++;
43 Sleep(10);
44 }
45 LeaveCriticalSection(&g_CriticalSection);
46 }
47
48 return(0);
49 }
50
51 void main(){
52 HANDLE hThread[2];
53
54 InitializeCriticalSection(&g_CriticalSection);
55
56 hThread[0]=CreateThread(
57 NULL,
58 0,
59 IncProc,
60 reinterpret_cast
下一篇:.net 错误处理