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

CreateEvent.cpp:

#include 
#include 


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


CreateMutex.cpp:

#include 
#include 

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


CriticalSection.cpp:

#include 
#include 

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




你可能感兴趣的:(c++/c)