#include <iostream>
#include <Windows.h>
#include <tchar.h>
using namespace std;
HANDLE hMutex;
//HANDLE CreateMutex(LPSECURITY_ATTRIBUTES lpMutexAttributes,
//BOOL bInitialOwner,
//LPCTSTR lpName);
// bInitialOwner:指定该资源初始时是否归属创建它的进程
// lpName:指定互斥量资源的名称
// BOOL ReleaseMutex( __in HANDLE hMutex );
// 用于释放一个独占资源,进程一旦释放该资源,该资源就不在属于它了,
// 如果还要用,需要重新申请得到该资源,申请资源函数
// DWORD WaitForSingleObject(HANDLE hHandle, DWORD dwMilliseconds);
DWORD WINAPI Fun(LPVOID pParam)
{
while(1)
{
WaitForSingleObject(hMutex, INFINITE);
cout << "Fun display!" << endl;
Sleep(100);
ReleaseMutex(hMutex);
}
}
int main()
{
HANDLE hThread = CreateThread(NULL, 0, Fun, NULL, 0, NULL);
// 创建一个名为screen的互斥量,并且初始时这个互斥量不属于创建
// 它的这个进程
hMutex = CreateMutex(NULL, FALSE, _T("screen"));
CloseHandle(hThread);
while(1)
{
WaitForSingleObject(hMutex, INFINITE);
cout << "main display!" << endl;
Sleep(200);
ReleaseMutex(hMutex);
}
system("pause");
return 0;
}
hMutex
最新推荐文章于 2021-11-10 10:48:45 发布