#include <iostream>
using namespace std;
#include <windows.h>
HANDLE hMutex;
DWORD WINAPI Fun(LPVOID lpParamter)
{
while(1)
{
WaitForSingleObject(hMutex, INFINITE);//允许一个线程拥有对共享资源的独占
cout<<"Fun display!"<<endl;
Sleep(1000);
ReleaseMutex(hMutex);
}
}
int main()
{
HANDLE hThread = CreateThread(NULL, 0, Fun, NULL, 0, NULL);
hMutex = CreateMutex(NULL, FALSE, "screen");//该函数用于创造一个独占资源,第一个参数我们没有使用,可以设为NULL,
//第二个参数指定该资源初始是否归属创建它的进程,第三个参数指定资源的名称
CloseHandle(hThread);
while(1)
{
WaitForSingleObject(hMutex, INFINITE);
cout<<"main display!"<<endl;
Sleep(2000);
ReleaseMutex(hMutex);
}
return 0;
}