C++线程同步方式及例子

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/daoming1112/article/details/54710649

CreateEvent.cpp:

#include <windows.h>
#include <stdio.h>


int WorkerID = 10;
const int MAXWORKERID = 100;
//Declare event handle
HANDLE hEvent;

//Define thread function
DWORD __stdcall ThreadFunOne(LPVOID lParam)
{
	for(;;)
	{
		WaitForSingleObject(hEvent,INFINITE);


		if (WorkerID<MAXWORKERID)
		{
			WorkerID +=1;
			Sleep(1000);
			printf("ThreadOne print out: %i \n",WorkerID);
		}
		SetEvent(hEvent);
	}
	return 0;
}

DWORD __stdcall ThreadFunTwo(LPVOID lParam)
{
	for (;;)
	{
		WaitForSingleObject(hEvent,INFINITE);		
		if(WorkerID<MAXWORKERID)
		{
			WorkerID +=1;		
			printf("ThreadTwo print out: %i \n",WorkerID);
			Sleep(1000);
		}
		SetEvent(hEvent);
	}
	return 0;
}

int main()
{
	//Define thread handle
	HANDLE hThread1,hThread2;
	//Create thread
	hThread1 = ::CreateThread(NULL,0,ThreadFunOne,NULL,0,NULL);
	hThread2 = ::CreateThread(NULL,0,ThreadFunTwo,NULL,0,NULL);

	//Create event
	hEvent = CreateEvent(NULL,FALSE,TRUE,"event");
	//Close thread handle
	CloseHandle(hThread1);
	CloseHandle(hThread2);
	//Note: Prevent process exiting
	while (true)
	{
		;
	}
	return 0;
}


CreateMutex.cpp:

#include <windows.h>
#include <stdio.h>

int WorkerID = 10;
const int MAXWORKERID = 50;
//Declare mutex handle
HANDLE hMutex;

//Define thread function
DWORD __stdcall ThreadFunOne(LPVOID lParam)
{
	for(;;)
	{
		WaitForSingleObject(hMutex,INFINITE);
		if (WorkerID<MAXWORKERID)
		{
			WorkerID +=1;
			Sleep(10);
			printf("ThreadOne print out: %i \n",WorkerID);
		}
		ReleaseMutex(hMutex);
	}
	return 0;
}

DWORD __stdcall ThreadFunTwo(LPVOID lParam)
{
	for (;;)
	{
		WaitForSingleObject(hMutex,INFINITE);
		if(WorkerID<MAXWORKERID)
		{
			WorkerID +=1;		
			printf("ThreadTwo print out: %i \n",WorkerID);
			Sleep(1000);
		}
		ReleaseMutex(hMutex);
	}
	return 0;
}

int main()
{
	//Define thread handle
	HANDLE hThread1,hThread2;
	//Create thread
	hThread1 = ::CreateThread(NULL,0,ThreadFunOne,NULL,0,NULL);
	hThread2 = ::CreateThread(NULL,0,ThreadFunTwo,NULL,0,NULL);

	//Create mutex
	hMutex = CreateMutex(NULL,false,"mutex");
	//Close thread handle
	CloseHandle(hThread1);
	CloseHandle(hThread2);
	//Note: Prevent process exiting
	while (true)
	{
		;
	}
	return 0;
}


CriticalSection.cpp:

#include <windows.h>
#include <stdio.h>

int WorkerID = 10;
const int MAXWORKERID = 100;
//Declare Critical Section 
CRITICAL_SECTION  Section;

//Define thread function
DWORD __stdcall ThreadFunOne(LPVOID lParam)
{
	for(;;)
	{
	//	EnterCriticalSection(&Section);
		if (WorkerID<MAXWORKERID)
		{
			WorkerID +=1;
			Sleep(1000);
			printf("ThreadOne print out: %i \n",WorkerID);
		}
	//	LeaveCriticalSection(&Section);
	}
	return 0;
}

DWORD __stdcall ThreadFunTwo(LPVOID lParam)
{
	for (;;)
	{
		EnterCriticalSection(&Section);
		if(WorkerID<MAXWORKERID)
		{
			WorkerID +=1;		
			printf("ThreadTwo print out: %i \n",WorkerID);
			Sleep(1000);
		}
		LeaveCriticalSection(&Section);
	}
	return 0;
}

int main()
{

	//Initialize critical section
	InitializeCriticalSection(&Section);
	//Define thread handle
	HANDLE hThread1,hThread2;
	//Create thread
	hThread1 = ::CreateThread(NULL,0,ThreadFunOne,NULL,0,NULL);
	hThread2 = ::CreateThread(NULL,0,ThreadFunTwo,NULL,0,NULL);

	//Close thread handle
	CloseHandle(hThread1);
	CloseHandle(hThread2);
	//Note: Prevent process exiting
	while (true)
	{
		;
	}
	return 0;
}




展开阅读全文

没有更多推荐了,返回首页