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

 

.

相关文章:

  • 2021-04-15
  • 2021-06-06
  • 2021-06-29
  • 2021-10-06
  • 2022-12-23
  • 2022-12-23
  • 2021-06-07
  • 2022-01-20
猜你喜欢
  • 2022-12-23
  • 2021-08-26
  • 2021-05-20
  • 2021-09-25
  • 2021-11-06
  • 2021-08-29
  • 2022-12-23
相关资源
相似解决方案