• 进程间互斥 Mutex


    对于普通的线程间互斥可以使用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;
    }
    

  • 相关阅读:
    centos下安装Anaconda
    centos下安装python2.7.9和pip以及数据科学常用的包
    mysql基础(5)-关联(mysql+pandas)
    mysql基础(4)-数据导入
    mysql基础(3)-高级查询
    mysql基础(2)-数据处理(mysql+pandas)
    mysql基础(1)-基本操作
    创建线程的三种方法
    Jar 包 及运行Jar包
    导出成可运行jar包时所遇问题的解决办法
  • 原文地址:https://www.cnblogs.com/hzcya1995/p/13318560.html
Copyright © 2020-2023  润新知