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

 

.


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM