1.CreateMutex()//创建一个互斥体,已经存在会直接返回句柄
OpenMutex()//打开一个互斥体
WaitForSingleObject()//尝试加锁
ReleaseMutex()//解锁
2.进程间互斥通过互斥锁的名称进行
3.如果是服务进程与普通进程进行通信要注意!!!!!!!!!!!!!!!!!!!
HANDLE hNutex = CreateMutex(NULL, FALSE, L"Local\\eee"); //创建本地内核对象
HANDLE hNutex = CreateMutex(NULL, FALSE, L"Global\\eee"); //创建全局内核对象
//进程1
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <windows.h>
using namespace std;
class MyClass
{
public:
MyClass(string lockName)
{
name = lockName;
hMutex = OpenMutex(MUTEX_ALL_ACCESS, NULL, name.c_str());
if (hMutex == NULL)
{
hMutex = CreateMutex(NULL, false, name.c_str());
std::cout << "CreateMutex" << "----------" << GetLastError() << std::endl;
}
};
~MyClass()
{
CloseHandle(hMutex);
hMutex = NULL;
};
void Lock()
{
while (WaitForSingleObject(hMutex, INFINITE) != WAIT_OBJECT_0) //INFINITE 阻塞方式,效率低
{
std::cout << "WaitForSingleObject" << "------------" << GetLastError() << std::endl;
Sleep(1);
};
}
void UnLock()
{
//hMutex = CreateMutex(NULL, false, name.c_str());
bool ret = ReleaseMutex(hMutex);
//CloseHandle(hMutex);
std::cout << "ReleaseMutex -----" << ret << "-------" << GetLastError() << std::endl;
}
private:
HANDLE hMutex;
string name;
};
static MyClass* myLock = new MyClass("mypmutex");
string GetTime()
{
SYSTEMTIME sys;
GetLocalTime(&sys);
char temp[256];
char name[256];
sprintf(temp, "%4d-%02d-%02d %02d:%02d:%02d-",
sys.wYear, sys.wMonth,
sys.wDay, sys.wHour,
sys.wMinute, sys.wSecond);
std::string inStr;
inStr += temp;
return inStr;
}
int main()
{
for (int i = 0;i < 10; i++)
{
// 申请对互斥量的占有
myLock->Lock();
// 模拟对公共内存/文件的操作
cout << GetTime() << "begin sleep" << endl;
//Sleep(5000);
cout << GetTime() << "process 111111111111111111111111111" << endl;
myLock->UnLock();
// 操作完毕,释放对互斥量的占有
cout << GetTime() << "reslease ok" << endl;
}
return 0;
}
//进程2
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <windows.h>
using namespace std;
class MyClass
{
public:
MyClass(string lockName)
{
name = lockName;
hMutex = OpenMutex(MUTEX_ALL_ACCESS, NULL, name.c_str());
if (hMutex == NULL)
{
hMutex = CreateMutex(NULL, false, name.c_str());
std::cout << "CreateMutex" << "----------" << GetLastError() << std::endl;
}
};
~MyClass()
{
CloseHandle(hMutex);
hMutex = NULL;
};
void Lock()
{
while (WaitForSingleObject(hMutex, INFINITE) != WAIT_OBJECT_0) //INFINITE 阻塞方式,效率低
{
std::cout << "WaitForSingleObject" << "------------" << GetLastError() << std::endl;
Sleep(1);
};
}
void UnLock()
{
//hMutex = CreateMutex(NULL, false, name.c_str());
bool ret = ReleaseMutex(hMutex);
//CloseHandle(hMutex);
std::cout << "ReleaseMutex -----" << ret << "-------" << GetLastError() << std::endl;
}
private:
HANDLE hMutex;
string name;
};
static MyClass* myLock = new MyClass("mypmutex");
string GetTime()
{
SYSTEMTIME sys;
GetLocalTime(&sys);
char temp[256];
char name[256];
sprintf(temp, "%4d-%02d-%02d %02d:%02d:%02d-",
sys.wYear, sys.wMonth,
sys.wDay, sys.wHour,
sys.wMinute, sys.wSecond);
std::string inStr;
inStr += temp;
return inStr;
}
int main()
{
for (int i = 0;i < 10; i++)
{
// 申请对互斥量的占有
myLock->Lock();
// 模拟对公共内存/文件的操作
cout << GetTime() << "begin sleep" << endl;
Sleep(10000);
cout << GetTime() << "process 2222222222222222222" << endl;
myLock->UnLock();
// 操作完毕,释放对互斥量的占有
cout << GetTime() << "reslease ok" << endl;
}
return 0;
}
参考:
1.https://blog.csdn.net/cainiaoxunchong/article/details/19089991
2.https://blog.csdn.net/u013659062/article/details/101086972
3.https://blog.csdn.net/icebergliu1234/article/details/104665904
进程间通信-互斥锁
最新推荐文章于 2024-07-25 23:49:52 发布