进程间互斥 Mutex

进程间互斥 Mutex

VC/C++ 2010-11-18 23:11:24 阅读148 评论0   字号: 订阅

        对于普通的线程间互斥可以使用CreateMutex传建一个匿名的互斥量做互斥,对 进程间的互斥就要用到命名互斥量来做互斥了。用到的函数有:
  1.  创建一个命名互斥量使用CreateMutex()方法,只需把lpName参数设置为非NULL,如"my mutex"
 HANDLE WINAPI CreateMutex(
           __in LPSECURITY_ATTRIBUTES lpMutexAttributes,
           __in BOOL bInitialOwner,
           __in LPCTSTR lpName);

2. 打开一个命名互斥量使用OpenMutex()方法,我们也需要对其中的lpName参数指定内容,如"my mutex"
HANDLE WINAPI OpenMutex(
          __in DWORD dwDesiredAccess,
          __in BOOL bInheritHandle,
          __in LPCTSTR lpName);

下面给出两段代码,可以同时使用两个process1或process1和process2查看运行效果
进程1的代码:
#include <iostream>
#include <windows.h>
using namespace std;


int main()
{
    /*
    SECURITY_ATTRIBUTES att;
    att.nLength = sizeof(SECURITY_ATTRIBUTES );
    att.lpSecurityDescriptor = NULL;
    att.bInheritHandle = TRUE;
    */

    HANDLE hMutex = CreateMutex(NULL, false, "pmutex");
    if(NULL == hMutex)
    {
        cout<<"create mutex error "<<GetLastError()<<endl;
        return 0;
    }
    else
    {
        cout<<" create mutex success:"<<hMutex<<endl;
    }

     for(int i = 0;i<10; i++)
    {
        DWORD  d  = WaitForSingleObject(hMutex, INFINITE);
        if(WAIT_OBJECT_0 == d)
        {
            cout<<"begin sleep"<<endl;
            Sleep(2000);
            cout<<"process 1"<<endl;
            if(ReleaseMutex(hMutex)!=0)
            {
                cout<<"reslease ok"<<endl;
            }
            else
            {
                cout<<"reslease failed"<<endl;
            }
        }
        if(WAIT_ABANDONED == d)
        {
            cout<<"WAIT_ABANDONED"<<endl;
        }
        if(WAIT_FAILED ==d)
        {
            cout<<"mutex error"<<endl;
        }
        Sleep(2000);
    }

    CloseHandle(hMutex);
    return 0;
}
  进程2 process2的代码
#include <iostream>
#include <windows.h>
using namespace std;

int main()
{
    HANDLE hMutex = OpenMutex(MUTEX_ALL_ACCESS, false, "pmutex");
    if(NULL == hMutex)
    {
        cout<<"open mutex error "<<GetLastError()<<endl;
        return 0;
    }
    else
    {
        cout<<"open mutex success:"<<hMutex<<endl;
    }

    for(int i = 0;i<10; i++)
    {
        DWORD  d  = WaitForSingleObject(hMutex, INFINITE);
        if(WAIT_OBJECT_0 == d)
        {
            cout<<"begin sleep"<<endl;
            Sleep(2000);
            cout<<"process 1"<<endl;
            if(ReleaseMutex(hMutex)!=0)
            {
                cout<<"reslease ok"<<endl;
            }
            else
            {
                cout<<"reslease failed"<<endl;
            }
        }
        if(WAIT_ABANDONED == d)
        {
            cout<<"WAIT_ABANDONED"<<endl;
        }
        if(WAIT_FAILED ==d)
        {
            cout<<"mutex error"<<endl;
        }
        Sleep(2000);
    }
    CloseHandle(hMutex);
    return 0;
}

   运行结果
进程间互斥 Mutex - strive_only - 奋斗,我要一直奋斗...
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值