进程间通信-互斥锁

1.CreateMutex()//创建一个互斥体,已经存在会直接返回句柄
OpenMutex()//打开一个互斥体
WaitForSingleObject()//尝试加锁
ReleaseMutex()//解锁
2.进程间互斥通过互斥锁的名称进行
3.如果是服务进程与普通进程进行通信要注意!!!!!!!!!!!!!!!!!!!
HANDLE hNutex = CreateMutex(NULL, FALSE, L"Local\\eee");  //创建本地内核对象 
HANDLE hNutex = CreateMutex(NULL, FALSE, L"Global\\eee");  //创建全局内核对象 

//进程1

#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <windows.h>
using namespace std;


class MyClass
{
public:
    MyClass(string lockName)
    {
        name = lockName;
        hMutex = OpenMutex(MUTEX_ALL_ACCESS, NULL, name.c_str());
        if (hMutex == NULL)
        {
            hMutex = CreateMutex(NULL, false, name.c_str());
            std::cout << "CreateMutex" << "----------" << GetLastError() << std::endl;
        }
    };
    ~MyClass()
    {
        CloseHandle(hMutex);
        hMutex = NULL;
    };

    void Lock()
    {
       
        
        while (WaitForSingleObject(hMutex, INFINITE) != WAIT_OBJECT_0) //INFINITE 阻塞方式,效率低
        {
            std::cout << "WaitForSingleObject" << "------------" << GetLastError() << std::endl;
            Sleep(1);
        };

    }

    void UnLock()
    {
        //hMutex = CreateMutex(NULL, false, name.c_str());
        bool ret = ReleaseMutex(hMutex);
        //CloseHandle(hMutex);
        std::cout << "ReleaseMutex -----" << ret << "-------" << GetLastError() << std::endl;
    }
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;
     
    }

    return 0;
}

//进程2


#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <windows.h>
using namespace std;


class MyClass
{
public:
    MyClass(string lockName)
    {
        name = lockName;

        hMutex = OpenMutex(MUTEX_ALL_ACCESS, NULL, name.c_str());
        if (hMutex == NULL)
        {
            hMutex = CreateMutex(NULL, false, name.c_str());
            std::cout << "CreateMutex" << "----------" << GetLastError() << std::endl;
        }

    };
    ~MyClass()
    {
        CloseHandle(hMutex);
        hMutex = NULL;
    };

    void Lock()
    {
        
        while (WaitForSingleObject(hMutex, INFINITE) != WAIT_OBJECT_0) //INFINITE 阻塞方式,效率低
        {
            std::cout << "WaitForSingleObject" << "------------" << GetLastError() << std::endl;
            Sleep(1);
        };

    }

    void UnLock()
    {
        //hMutex = CreateMutex(NULL, false, name.c_str());
        bool ret = ReleaseMutex(hMutex);
        //CloseHandle(hMutex);
        std::cout << "ReleaseMutex -----" << ret << "-------" << GetLastError() << std::endl;
    }
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(10000);
        cout << GetTime() << "process 2222222222222222222" << endl;
        myLock->UnLock();
        // 操作完毕,释放对互斥量的占有
        cout << GetTime() << "reslease ok" << endl;

    }

    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
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值