当前位置:网站首页>Interprocess communication - mutex
Interprocess communication - mutex
2022-04-23 06:44:00 【The old man is outside the door】
CreateMutex()// Create a mutex , If it already exists, it will directly return the handle
OpenMutex()// Open a mutex
WaitForSingleObject()// Try to lock
ReleaseMutex()// Unlock
Inter process mutex is defined by the name of the mutex
// process 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 Blocking mode , Low efficiency
{
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++)
{
// Apply for possession of mutex
myLock->Lock();
// Analog to common memory / Operation of file
cout << GetTime() << "begin sleep" << endl;
Sleep(5000);
cout << GetTime() << "process 2222222222222222222" << endl;
myLock->UnLock();
// Operation completed , Release the possession of mutex
cout << GetTime() << "reslease ok" << endl;
Sleep(2000);
}
return 0;
}
// process 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 Blocking mode , Low efficiency
{
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++)
{
// Apply for possession of mutex
myLock->Lock();
// Analog to common memory / Operation of file
cout << GetTime() << "begin sleep" << endl;
Sleep(5000);
cout << GetTime() << "process 111111111111111111111111111" << endl;
myLock->UnLock();
// Operation completed , Release the possession of mutex
cout << GetTime() << "reslease ok" << endl;
Sleep(2000);
}
return 0;
}
Reference resources :
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
版权声明
本文为[The old man is outside the door]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230548038625.html
边栏推荐
猜你喜欢
【UDS统一诊断服务】一、诊断概述(1)— 诊断概述
进程管理命令
FOC 单电阻采样 位置环控制伺服电机
C语言的浪漫
gcc ,g++,gdb的安装
CUDA环境安装
CUDA environment installation
Qt 添加QSerialPort类 实现串口操作
Collection of practical tips for C language (continuously updated)
[UDS unified diagnostic service] IV. typical diagnostic service (5) - function / component test function unit (routine function unit 0x31)
随机推荐
[UDS unified diagnostic service] III. application layer protocol (1)
【UDS统一诊断服务】四、诊断典型服务(1)— 诊断和通信管理功能单元
Protection of shared data
copy constructor
基于SSD的物体检测案例实现
[stepping on the pit] MELD in win11 wsl2 cannot be used normally. Problem repair
日志写法(带时间)
Opencv uses genericindex for KNN search
【UDS统一诊断服务】一、诊断概述(2)— 主要诊断协议(K线和CAN)
对象数组与对象指针
Shell脚本 单引号、双引号和反引号的区别
TensorFlow张量介绍
【UDS统一诊断服务】五、诊断应用示例:Flash Bootloader
[learn] HF net training
修改注册表的值
【UDS统一诊断服务】三、应用层协议(2)
C#【文件操作篇】PDF文件和图片互相转换
Qt 给应用程序加图标
Shell脚本的通配符和特殊符号
文件查看命令和用户管理命令