zoukankan      html  css  js  c++  java
  • WIN32互斥体CreateMutex以及限制多开

    CreateMutex(
    
    LPSECURITY_ATTRIBUTES 【lpMutexAttributes】,    //指向安全属性的指针
    BOOL 【bInitialOwner】,    //标志初始所有权
    LPCTSTR 【lpName】    //指向mutex对象名称的指针
    );
    // Mutex0616.cpp : Defines the entry point for the console application.
    //
    
    #include "stdafx.h"
    #include <WINDOWS.H>
    int main(int argc, char* argv[])
    {
        //创建互斥体
        HANDLE hMutex = CreateMutex(NULL,FALSE,"xyz");
        //获取令牌
        WaitForSingleObject(hMutex,INFINITE);
    
        for (int i =0;i<10;i++)
        {
            Sleep(1000);
            printf("A进程的X线程:%d\n",i);
        }
    
        ReleaseMutex(hMutex);
    
        return 0;
    }
    // Mutex0616.cpp : Defines the entry point for the console application.
    //
    
    #include "stdafx.h"
    #include <WINDOWS.H>
    int main(int argc, char* argv[])
    {
        //创建互斥体
        HANDLE hMutex = CreateMutex(NULL,FALSE,"xyz");
        //获取令牌
        WaitForSingleObject(hMutex,INFINITE);
        
        for (int i =0;i<10;i++)
        {
            Sleep(1000);
            printf("B进程的Y线程:%d\n",i);
        }
        
        ReleaseMutex(hMutex);
        
        return 0;
    }

     

    // Mutex0616.cpp : Defines the entry point for the console application.
    //
    
    #include "stdafx.h"
    #include <WINDOWS.H>
    int main(int argc, char* argv[])
    {
        //创建互斥体
        HANDLE hMutex = CreateMutex(NULL,FALSE,"xyz");
    
        DWORD dret = GetLastError();
        if(hMutex)
        {
            if (ERROR_ALREADY_EXISTS == dret)
            {
                CloseHandle(hMutex);
                return 0;
            }
        }
        else
        {
            printf("创建失败!程序退出!");
            CloseHandle(hMutex);
            return 0;
        }
        //获取令牌
        //WaitForSingleObject(hMutex,INFINITE);
        
        for (;;)
        {
            Sleep(1000);
            printf("A进程的X线程\n");
        
        }
        //ReleaseMutex(hMutex);
        
        return 0;
    }

    .

  • 相关阅读:
    中国大概可用NTPserver地址
    ROOT android 原则。 基于(zergRush)
    Struts2他们拦截器实例定义—登陆权限验证
    引用与指针
    address_space 从哪里来
    C++ 可以多个函数声明
    linux下远程管理利器-tmux
    内核空间和用户空间的分界 PAGE_OFFSET
    io端口
    C中程序的内存分配
  • 原文地址:https://www.cnblogs.com/ganxiang/p/13141958.html
Copyright © 2011-2022 走看看