MFC 线程学习笔记之临界区

//临界区
int number=1;
CRITICAL_SECTION  Critical;//定义临界区句柄
unsigned long _stdcall ThreadProc3(void* lpParameter)
{
	long count;
while(number<100)
{
	EnterCriticalSection(&Critical);
	printf("线程3当前计数:%d\n",number);
	number++;
	Sleep(1000);
	LeaveCriticalSection(&Critical);
}
return 0;
}

unsigned long _stdcall ThreadProc4(void* lpParameter)
{
	long count;
while(number<100)
{
	EnterCriticalSection(&Critical);
	printf("线程4当前计数:%d\n",number);
	number++;
	Sleep(1000);
	LeaveCriticalSection(&Critical);
}
return 0;
}

int _tmain(int argc, _TCHAR* argv[])
{
	InitializeCriticalSection(&Critical);
	HANDLE hThread3=CreateThread(NULL,0,ThreadProc3,NULL,0,NULL);
	HANDLE hThread4=CreateThread(NULL,0,ThreadProc4,NULL,0,NULL);
	CloseHandle(hThread3);
	CloseHandle(hThread4);
	while(TRUE)
	{
	 ;
	}
	return 0;
}


你可能感兴趣的:(MFC 线程学习笔记之临界区)