当前位置:网站首页>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)— 诊断和通信管理功能单元
PHP junior programmers, take orders and earn extra money
【UDS统一诊断服务】四、诊断典型服务(2)— 数据传输功能单元
在visual stdio中运行qt程序
[UDS unified diagnosis service] IV. typical diagnosis service (3) - read fault information function unit (storage data transmission function unit)
[ThreadX] h743zi + lan8720 + ThreadX + netx duo transplantation
[UDS unified diagnosis service] i. diagnosis overview (3) - ISO 15765 architecture
[UDS unified diagnostic service] II. Network layer protocol (2) - data transmission rules (single frame and multi frame)
[UDS] unified diagnostic service (UDS)
进程管理命令
随机推荐
Static member
深蓝学院激光slam 理论与实践 第三章激光雷达去畸变 作业习题
【UDS统一诊断服务】五、诊断应用示例:Flash Bootloader
[UDS unified diagnostic service] i. overview of diagnosis (4) - basic concepts and terms
[UDS unified diagnosis service] IV. typical diagnosis service (1) - diagnosis and communication management function unit
QT add qserialport class to realize serial port operation
VHDL 有限状态机(FSM) 代码示例
【UDS统一诊断服务】(补充)五、ECU bootloader开发要点详解 (1)
C语言 #和##的使用
【UDS统一诊断服务】一、诊断概述(2)— 主要诊断协议(K线和CAN)
sqlite编译
Eigen 学习总结
CUDA环境安装
TP download folder, compress folder and download
【UDS统一诊断服务】四、诊断典型服务(5)— 功能/元件测试功能单元(例行程序功能单元0x31)
C语言的浪漫
Opencv uses genericindex for KNN search
[ThreadX] ThreadX source code reading plan (I)
cv_bridge 与opencv 版本不匹配的解决
[stepping on the pit] MELD in win11 wsl2 cannot be used normally. Problem repair