3个线程同步的使用例子:
1.互斥量 Mutex
HANDLE m_hMutex;
hMutex = CreateMutex(NULL,FALSE,NULL);// 创建一个匿名的互斥对象
WaitForSingleObject(hMutex,INFINITE);
...被保护的代码段
ReleaseMutex(hMutex);
2.事件 Event
HANDLE g_hEvent;
g_hEvent = CreateEvent(NULL,FALSE,FALSE,NULL);// 创建 人工重置事件对象 初始状态为无信号 匿名
SetEvent(g_hEvent);// 设置 g_hEvent 有信号
WaitForSingleObject(g_hEvent,INFINITE);// 请求到事件后 将 g_hEvent 设置为无信号
...被保护的代码段
SetEvent(g_hEvent);// 处理完成后,将事件设置为有信号的
3.临界区 Critical
CRITICAL_SECTION g_cs;//声明临界区对象
//初始化临界区对象
InitializeCriticalSection(&g_cs);
EnterCriticalSection(&g_cs);
... 保护的代码
LeaveCriticalSection(&g_cs);
DeleteCriticalSection(&g_cs);
CreateThread 创建线程
CreateMutex 创建互斥对象
WaitForSingleObject(hMutex,INFINITE)
....中间是要被保护的代码
ReleaseMutex(hMutex)
//--------------------------------------------------------------------------------------------------
//
// 互斥量 Mutex
//
//--------------------------------------------------------------------------------------------------
#include
#include
using namespace std;
DWORD WINAPI Fun1Proc(LPVOID lpParameter);
DWORD WINAPI Fun2Proc(LPVOID lpParameter);
int index = 0;
int tickets = 100;
HANDLE hMutex;
void main()
{
HANDLE hThread1,hThread2;
hThread1 = CreateThread(NULL,0,Fun1Proc,NULL,0,NULL);
CloseHandle(hThread1);
hThread2 = CreateThread(NULL,0,Fun2Proc,NULL,0,NULL);
CloseHandle(hThread2);
/*while (index++ < 1000)
cout<< "main thread is running " << endl;*/
hMutex = CreateMutex(NULL,FALSE,NULL);// 创建一个匿名的互斥对象
/*if (ERROR_ALREADY_EXISTS == GetLastError())
{
cout<< "only instance one" << endl;
system("pause");
return;
}*/
WaitForSingleObject(hMutex,INFINITE);
ReleaseMutex(hMutex);
ReleaseMutex(hMutex);
Sleep(4000);
system("pause");
}
DWORD WINAPI Fun1Proc(LPVOID lpParameter)
{
/*while (index++ < 1000)
cout<< "thread1 is running" << endl;*/
while (TRUE)
{
WaitForSingleObject(hMutex,INFINITE);
if (tickets > 0)
{
Sleep(1);
cout<< "Thread1 sell ticket" << tickets-- << endl;
}
else
{
break;
}
ReleaseMutex(hMutex);
}
return 0;
}
DWORD WINAPI Fun2Proc(LPVOID lpParameter)
{
while (TRUE)
{
WaitForSingleObject(hMutex,INFINITE);
if (tickets > 0)
{
Sleep(1);
cout<< "Thread2 sell ticket" << tickets-- << endl;
}
else
{
break;
}
ReleaseMutex(hMutex);
}
return 0;
}
事件对象也属于内核对象,包含一个使用计数,一个用于指明该事件是一个自动重置的事件还是一个用于人工重置的事件的布尔值,
另一个用于指明该事件处于已通知状态还是未通知状态的布尔值
CreateEvent 创建一个事件对象
SetEvent 将事件对象设置为有信号状态
//--------------------------------------------------------------------------------------------------
//
// 事件 Event
//
//--------------------------------------------------------------------------------------------------
#include
#include
using namespace std;
DWORD WINAPI Fun1Proc(LPVOID lpParameter);
DWORD WINAPI Fun2Proc(LPVOID lpParameter);
int tickets = 100;
HANDLE g_hEvent;
void main()
{
HANDLE hThread1,hThread2;
hThread1 = CreateThread(NULL,0,Fun1Proc,NULL,0,NULL);
hThread2 = CreateThread(NULL,0,Fun2Proc,NULL,0,NULL);
CloseHandle(hThread1);
CloseHandle(hThread2);
// 创建 人工重置事件对象 初始状态为无信号 匿名
g_hEvent = CreateEvent(NULL,FALSE,FALSE,NULL);
// 设置 g_hEvent 有信号
SetEvent(g_hEvent);
Sleep(4000);
CloseHandle(g_hEvent);
system("pause");
}
DWORD WINAPI Fun1Proc(LPVOID lpParameter)
{
while (TRUE)
{
// 请求到事件后 将 g_hEvent 设置为无信号
WaitForSingleObject(g_hEvent,INFINITE);
if (tickets > 0)
{
Sleep(1);
cout<< "Thread1 sell ticket: " << tickets-- << endl;
}
else
{
break;
}
SetEvent(g_hEvent);
}
return 0;
}
DWORD WINAPI Fun2Proc(LPVOID lpParameter)
{
while (TRUE)
{
WaitForSingleObject(g_hEvent,INFINITE);
if (tickets > 0)
{
Sleep(1);
cout<< "Thread2 sell ticket: " << tickets-- << endl;
}
else
{
break;
}
SetEvent(g_hEvent);
}
return 0;
}
//--------------------------------------------------------------------------------------------------
//
// 临界区 InitializeCriticalSection / EnterCriticalSection / LeaveCriticalSection
//
//--------------------------------------------------------------------------------------------------
CRITICAL_SECTION g_cs;//声明临界区对象
//初始化临界区对象
InitializeCriticalSection(&g_cs);
DeleteCriticalSection(&g_cs);
EnterCriticalSection(&g_cs);
... 保护的代码
LeaveCriticalSection(&g_cs);
#include
#include
#include
using namespace std;
DWORD WINAPI Fun1Proc(LPVOID lpParameter);
DWORD WINAPI Fun2Proc(LPVOID lpParameter);
int tickets = 100;
CRITICAL_SECTION g_cs;//声明临界区对象
void main()
{
HANDLE hThread1,hThread2;
hThread1 = CreateThread(NULL,0,Fun1Proc,NULL,0,NULL);
hThread2 = CreateThread(NULL,0,Fun2Proc,NULL,0,NULL);
CloseHandle(hThread1);
CloseHandle(hThread2);
//初始化临界区对象
InitializeCriticalSection(&g_cs);
Sleep(4000);
DeleteCriticalSection(&g_cs);
system("pause");
}
DWORD WINAPI Fun1Proc(LPVOID lpParameter)
{
while (TRUE)
{
// 进入临界区
EnterCriticalSection(&g_cs);
if (tickets > 0)
{
Sleep(1);
cout<< "Thread1 sell ticket: " << tickets-- << endl;
}
else
{
break;
}
// 离开临界区
LeaveCriticalSection(&g_cs);
}
return 0;
}
DWORD WINAPI Fun2Proc(LPVOID lpParameter)
{
while (TRUE)
{
EnterCriticalSection(&g_cs);
if (tickets > 0)
{
Sleep(1);
cout<< "Thread2 sell ticket: " << tickets-- << endl;
}
else
{
break;
}
LeaveCriticalSection(&g_cs);
}
return 0;
}