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