// ConsoleApplication1.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include <windows.h>
#include <stdlib.h>
int g_nNum = 0;
HANDLE g_hMutex = nullptr;
DWORD WINAPI ThreadProc(LPVOID lpParam)
{
for (int i = 0; i < 5; i++)
{
WaitForSingleObject(g_hMutex, INFINITE);
printf("%d ", g_nNum++);
ReleaseMutex(g_hMutex);
}
return 0;
}
int _tmain(int argc, _TCHAR* argv[])
{
if (!(g_hMutex = CreateMutex(NULL, FALSE, NULL)))
return 0;
CreateThread(NULL, 0, ThreadProc, NULL, 0, nullptr);
CreateThread(NULL, 0, ThreadProc, NULL, 0, nullptr);
system("pause");
return 0;
}