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;
    }

    .

  • 相关阅读:
    Android开发之深入理解NFC(一)
    NetBeans找不到C/C++编译器
    【图解HTTP】第二章 简单的http协议
    长时间停留在calculating requirements and dependencies
    【图解HTTP】第一章 了解web及网络基础
    自定义DropDownMenu菜单
    【Android开发精要笔记】Android的Intent机制
    【操作系统】进程管理
    【Head First Java 读书笔记】(七)继承
    网易电面题总结
  • 原文地址:https://www.cnblogs.com/ganxiang/p/13141958.html
Copyright © 2011-2022 走看看