MFC学习笔记 线程之信号量

MFC学习笔记 线程之信号量_第1张图片

int number=1;
HANDLE hDemaphone;
unsigned long _stdcall ThreadProc3(void* lpParameter)
{
	long count;
while(number<100)
{
	WaitForSingleObject(hDemaphone,INFINITE);
	printf("线程3当前计数:%d\n",number);
	number++;
	Sleep(100);
	ReleaseSemaphore(hDemaphone,1,&count);
}
return 0;
}

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

int _tmain(int argc, _TCHAR* argv[])
{
	HANDLE hThread3=CreateThread(NULL,0,ThreadProc3,NULL,0,NULL);
	HANDLE hThread4=CreateThread(NULL,0,ThreadProc4,NULL,0,NULL);
	hDemaphone=CreateSemaphore(NULL,1,100,(LPCWSTR)"sem");
	CloseHandle(hThread3);
	CloseHandle(hThread4);
	while(TRUE)
	{
	 ;
	}
	return 0;
}


你可能感兴趣的:(MFC学习笔记 线程之信号量)