当前位置:网站首页>进程间通信-互斥锁
进程间通信-互斥锁
2022-04-23 05:48:00 【老朽在门外】
CreateMutex()//创建一个互斥体,已经存在会直接返回句柄
OpenMutex()//打开一个互斥体
WaitForSingleObject()//尝试加锁
ReleaseMutex()//解锁
进程间互斥通过互斥锁的名称进行
//进程1
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <windows.h>
using namespace std;
class MyClass
{
public:
MyClass(string lockName)
{
name= lockName;
};
~MyClass()
{
CloseHandle(hMutex);
hMutex = NULL;
};
void Lock()
{
hMutex = CreateMutex(NULL, false, name.c_str());
while (WaitForSingleObject(hMutex, INFINITE) != WAIT_OBJECT_0) //INFINITE 阻塞方式,效率低
{
Sleep(1);
};
}
void UnLock()
{
hMutex = CreateMutex(NULL, false, name.c_str());
ReleaseMutex(hMutex);
}
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 2222222222222222222" << endl;
myLock->UnLock();
// 操作完毕,释放对互斥量的占有
cout << GetTime() << "reslease ok" << endl;
Sleep(2000);
}
return 0;
}
//进程2
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <windows.h>
using namespace std;
class MyClass
{
public:
MyClass(string lockName)
{
name = lockName;
};
~MyClass()
{
CloseHandle(hMutex);
hMutex = NULL;
};
void Lock()
{
hMutex = CreateMutex(NULL, false, name.c_str());
while (WaitForSingleObject(hMutex, INFINITE) != WAIT_OBJECT_0) //INFINITE 阻塞方式,效率低
{
Sleep(1);
};
}
void UnLock()
{
hMutex = CreateMutex(NULL, false, name.c_str());
ReleaseMutex(hMutex);
}
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;
Sleep(2000);
}
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
版权声明
本文为[老朽在门外]所创,转载请带上原文链接,感谢
https://blog.csdn.net/weixin_41167925/article/details/124335921
边栏推荐
- POJ - 2955 brackets interval DP
- 识别验证码
- PM2 deploy nuxt project
- Techniques et principes de détection
- 根据SQL语句查询出的结果集,将其封装为json
- Record the installation and configuration of gestermer on TX2, and then use GST RTSP server
- selenium+PhantomJS破解滑动验证2
- 【UDS统一诊断服务】四、诊断典型服务(3)— 读故障信息功能单元(存储数据传输功能单元)
- Formation à la programmation
- 类和对象的初始化(构造函数与析构函数)
猜你喜欢
随机推荐
Rust 中的 RefCell
How SYSTEMd uses / etc / init D script
Advanced operation of idea debug
日志
爬虫效率提升方法
利用文件保存数据(c语言)
selenium+PhantomJS破解滑动验证2
1006 finding a mex (hdu6756)
Rust:如何 match 匹配 String 字符串?
Kalman filter and inertial integrated navigation
识别验证码
深拷贝和浅拷贝的区别
LockSupport. Park and unpark, wait and notify
D. Optimal partition segment tree optimization DP
Qthread simple test understanding
定位器
Cf1427c the hard work of paparazzi
Detection technology and principle
Log4j2跨线程打印traceId
Programming training




![[leetcode 202] happy number](/img/b2/a4e65688aef3a0cec8088bcaba048f.jpg)




