当前位置:网站首页>进程间通信-互斥锁
进程间通信-互斥锁
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
边栏推荐
猜你喜欢

解决ArcGIS分区统计显示太多唯一值执行失败

Completely clean up MySQL win

Understanding and installing MySQL

Robocode教程5——Enemy类

clion安装教程

Advanced operation of idea debug

1007 go running (hdu6808) in the fourth game of 2020 Hangzhou Electric Multi school competition

-- SQL query and return limit rows

Swagger2 generates API documents

MySQL table constraints and table design
随机推荐
clion安装教程
[leetcode 350] intersection of two arrays II
C array
数组旋转
word排版遇到的格式问题
爬取小米有品app商品数据
Excel打开超大csv格式数据
Integers have friends interval GCD + double pointer
Installation and usage skills of idea
基于QQwebAPI 查询昵称和头像的爬虫
Kalman filter and inertial integrated navigation
Explanation of login page
Conversion between JS object and string
Feign请求日志统一打印
利用文件保存数据(c语言)
Mysql database foundation
PM2 deploy nuxt project
7-21日错题涉及知识点。
Rust 中的 Rc智能指针
用C语言实现重写strcmp等四个函数