#include
#include
using namespace std;
int ticket = 100;
HANDLE hMutex; //定义互斥对象
DWORD WINAPI FuncProc1(LPVOID lpParameter);
DWORD WINAPI FuncProc2(LPVOID lpParameter);
int main()
{
hMutex = CreateMutex(NULL, FALSE, NULL); //定义互斥对象,并让该线程不拥有它
HANDLE hThread1 = CreateThread(NULL, 0, FuncProc1, NULL, 0, NULL);
HANDLE hThread2 = CreateThread(NULL,0, FuncProc2, NULL, 0, NULL);
CloseHandle(hThread1);
CloseHandle(hThread2);
Sleep(1000); //让主线程睡眠1秒
return 0;
}
DWORD WINAPI FuncProc1(LPVOID lpParameter) {
while (true) {
WaitForSingleObject(hMutex, INFINITE);
if (ticket > 0) {
Sleep(1);
cout << "ticket1 :" << ticket-- << endl;
}
else
break;
ReleaseMutex(hMutex);
}
return 0;
}
DWORD WINAPI FuncProc2(LPVOID lpParameter) {
while (true) {
WaitForSingleObject(hMutex, INFINITE); //申请互斥对象的所有权
if (ticket > 0) {
Sleep(1);
cout << "ticket2 :" << ticket-- << endl;
}
else
break;
ReleaseMutex(hMutex); //释放互斥对象的所有权
}
return 0;
}
注意:
虽然改程序运行结果是某一条线程执行完然后第二条线程执行,如此往复,但是这不是同步,因为我们没法控制到底一开始是谁先执行,(我们只是控制了轮流次序)。
#include
#include
using namespace std;
int ticket = 100;
HANDLE g_hEvent;
DWORD WINAPI FuncProc1(LPVOID lpParameter);
DWORD WINAPI FuncProc2(LPVOID lpParameter);
int main()
{
//参数意义:默认安全性,自动重置事件,初始时该事件对象就有信号
g_hEvent = CreateEvent(NULL, FALSE, TRUE, NULL);
HANDLE hThread1 = CreateThread(NULL, 0, FuncProc1, NULL, 0, NULL);
HANDLE hThread2 = CreateThread(NULL,0, FuncProc2, NULL, 0, NULL);
CloseHandle(hThread1);
CloseHandle(hThread2);
Sleep(1000); //让主线程睡眠1秒
CloseHandle(g_hEvent);
return 0;
}
DWORD WINAPI FuncProc1(LPVOID lpParameter) {
while (true) {
WaitForSingleObject(g_hEvent, INFINITE); //申请事件对象(申请钥匙,持有钥匙)
if (ticket > 0) {
Sleep(1);
cout << "ticket1 :" << ticket-- << endl;
SetEvent(g_hEvent); //(放弃钥匙,不在拥有)
}
else
{
SetEvent(g_hEvent); //设置为有信号
break;
}
}
return 0;
}
DWORD WINAPI FuncProc2(LPVOID lpParameter) {
while (true) {
WaitForSingleObject(g_hEvent, INFINITE); //申请事件对象(申请钥匙,持有钥匙)
if (ticket > 0) {
Sleep(1);
cout << "ticket2 :" << ticket-- << endl;
SetEvent(g_hEvent); //(放弃钥匙,不在拥有)
}
else
{
SetEvent(g_hEvent); //设置为有信号
break;
}
}
return 0;
}
改进:具有严格的同步控制
#include
#include
using namespace std;
int ticket = 100;
HANDLE g_hEvent1;
HANDLE g_hEvent2;
DWORD WINAPI FuncProc1(LPVOID lpParameter);
DWORD WINAPI FuncProc2(LPVOID lpParameter);
int main()
{
g_hEvent1 = CreateEvent(NULL, FALSE, TRUE, NULL); //创建事件对象
g_hEvent2 = CreateEvent(NULL, FALSE, FALSE, NULL); //创建事件对象
HANDLE hThread1 = CreateThread(NULL, 0, FuncProc1, NULL, 0, NULL);
HANDLE hThread2 = CreateThread(NULL,0, FuncProc2, NULL, 0, NULL);
CloseHandle(hThread1);
CloseHandle(hThread2);
Sleep(1000); //让主线程睡眠1秒
CloseHandle(g_hEvent1);
CloseHandle(g_hEvent2);
return 0;
}
DWORD WINAPI FuncProc1(LPVOID lpParameter) {
while (true) {
WaitForSingleObject(g_hEvent1, INFINITE); //申请事件对象(申请钥匙,持有钥匙)
if (ticket > 0) {
Sleep(1);
cout << "ticket1 :" << ticket-- << endl;
SetEvent(g_hEvent2); //让线程2执行
}
else
{
SetEvent(g_hEvent2); //让线程2执行
break;
}
}
return 0;
}
DWORD WINAPI FuncProc2(LPVOID lpParameter) {
while (true) {
WaitForSingleObject(g_hEvent2, INFINITE); //申请事件对象(申请钥匙,持有钥匙)
if (ticket > 0) {
Sleep(1);
cout << "ticket2 :" << ticket-- << endl;
SetEvent(g_hEvent1); //让线程1执行
}
else
{
SetEvent(g_hEvent1); //让线程1执行
break;
}
}
return 0;
}
#include
#include "windows.h"
using namespace std;
DWORD WINAPI FunProc1(LPVOID lpParameter);
DWORD WINAPI FunProc2(LPVOID lpParameter);
int ticket = 1000;
CRITICAL_SECTION g_cs;
int main()
{
InitializeCriticalSection(&g_cs); //初始化临界区
HANDLE hThread1 = CreateThread(NULL, 0, FunProc1, NULL, 0, NULL);
HANDLE hThread2 = CreateThread(NULL, 0, FunProc2, NULL, 0, NULL);
CloseHandle(hThread1);
CloseHandle(hThread2);
Sleep(1000);
DeleteCriticalSection(&g_cs); //删除临界区
return 0;
}
DWORD WINAPI FunProc1(LPVOID lpParameter)
{
while (TRUE) {
EnterCriticalSection(&g_cs); //进入临界区
Sleep(1);
if (ticket > 0) {
Sleep(1);
cout << "ticket1: " << ticket-- << endl;
LeaveCriticalSection(&g_cs); //离开临界区
}
else {
LeaveCriticalSection(&g_cs); //离开
break;
}
}
return 0;
}
DWORD WINAPI FunProc2(LPVOID lpParameter)
{
while (TRUE) {
EnterCriticalSection(&g_cs);
Sleep(1);
if (ticket > 0) {
Sleep(1);
cout << "ticket2: " << ticket-- << endl;
LeaveCriticalSection(&g_cs);
}
else {
LeaveCriticalSection(&g_cs);
break;
}
}
return 0;
}
https://blog.csdn.net/ebowtang/article/details/29905309