当前位置:网站首页>进程间通信-互斥锁
进程间通信-互斥锁
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
边栏推荐
猜你喜欢
【无标题】
SQL -- data filtering and grouping
1007 go running (hdu6808) in the fourth game of 2020 Hangzhou Electric Multi school competition
解决ArcGIS分区统计显示太多唯一值执行失败
How SYSTEMd uses / etc / init D script
Techniques et principes de détection
[leetcode 54] spiral matrix
-- SQL query and return limit rows
Understanding and installing MySQL
jenkspy包安装
随机推荐
队列解决约瑟夫问题
Qthread simple test understanding
斯坦福机器学习课程汇总
Linux 用rpm的方式安装mysql(超简单)
Rust:单元测试(cargo test )的时候显示 println 的输出信息
爬虫之requests基本用法
Rust 中的 Rc智能指针
Completely clean up MySQL win
Option的正确打开方式
Integration and induction of knowledge points of automatic control principle (Han min version)
Feign请求日志统一打印
GDB debugger installation and use
爬取彩票数据
爬取蝉妈妈数据平台商品数据
进程间通信的方式
Rust的闭包类型(Fn, FnMut, FnOne的区别)
利用文件保存数据(c语言)
Robocode教程5——Enemy类
[leetcode 401] binary Watch
Definition of C class and method