There are many Mutex operations among multiple threads, including the Critical Section, Mutex, Semaphore, and Event.
Among them, the critical section, mutex amount, and semaphores are strictly implemented for mutex operations, and events should be considered as a communication mechanism between threads to ensure mutex.
In multithreading, you can define these variables as global variables and use them in different threads. Therefore, multi-process environments will not work.
Multi-process Mutex operations can only be used for multi-shared resources)
Mutex is divided into naming and anonymous Mutex. Only the naming method can be used between processes.
The operation functions in windows are: CreateMutex, WaitForSingleObject, ReleaseMutex, CloseHandle
In linux, the operation functions are sem_open, sem_wait, sem_post, and sem_close (the mutex handle in the current process is closed and exists in the kernel) and sem_unlink (remove mutex from the kernel)
The following encapsulates a cross-platform class that implements multi-process mutex operations. I tested it myself and can use it:
ProcessMutex. h file:
1 # ifndef _ PROCESS_MUTEX_H _ 2 # define _ PROCESS_MUTEX_H _ 3 4 # ifdef WIN32 5 # include <Windows. h> 6 # endif 7 8 # ifdef linux 9 # include <unistd. h> 10 # include <semaphore. h> 11 # include <stdio. h> 12 # include <fcntl. h> 13 # include <signal. h> 14 # include <string. h> 15 # include <memory. h> 16 # endif17 18 class CProcessMutex19 {20 public: 21/* anonymous mutex created by default */22 CProcessMutex (const char * name = NULL); 23 ~ CProcessMutex (); 24 25 bool Lock (); 26 bool UnLock (); 27 28 private: 29 30 # ifdef WIN3231 void * m_pMutex; 32 # endif33 34 # ifdef linux35 sem_t * m_pSem; 36 # endif37 char m_cMutexName [30]; 38}; 39 40 # endif
ProcessMutex. cpp file:
1 # include "ProcessMutex. h "2 3 # ifdef WIN32 4 5 CProcessMutex: CProcessMutex (const char * name) 6 {7 memset (m_cMutexName, 0, sizeof (m_cMutexName )); 8 int min = strlen (name)> (sizeof (m_cMutexName)-1 )? (Sizeof (m_cMutexName)-1): strlen (name); 9 strncpy (m_cMutexName, name, min); 10 m_pMutex = CreateMutex (NULL, false, m_cMutexName ); 11} 12 13 CProcessMutex ::~ CProcessMutex () 14 {15 CloseHandle (m_pMutex); 16} 17 18 bool CProcessMutex: Lock () 19 {20 // mutex Lock creation failure 21 if (NULL = m_pMutex) 22 {23 return false; 24} 25 26 DWORD nRet = WaitForSingleObject (m_pMutex, INFINITE); 27 if (nRet! = WAIT_OBJECT_0) 28 {29 return false; 30} 31 32 return true; 33} 34 35 bool CProcessMutex: UnLock () 36 {37 return ReleaseMutex (m_pMutex ); 38} 39 40 # endif41 42 # ifdef linux43 44 CProcessMutex: CProcessMutex (const char * name) 45 {46 memset (m_cMutexName, 0, sizeof (m_cMutexName )); 47 int min = strlen (name)> (sizeof (m_cMutexName)-1 )? (Sizeof (m_cMutexName)-1): strlen (name); 48 strncpy (m_cMutexName, name, min); 49 m_pSem = sem_open (name, O_RDWR | O_CREAT, 0644, 1 ); 50} 51 52 CProcessMutex ::~ CProcessMutex () 53 {54 int ret = sem_close (m_pSem); 55 if (0! = Ret) 56 {57 printf ("sem_close error % d \ n", ret); 58} 59 sem_unlink (m_cMutexName); 60} 61 62 bool CProcessMutex: Lock () 63 {64 int ret = sem_wait (m_pSem); 65 if (ret! = 0) 66 {67 return false; 68} 69 return true; 70} 71 72 bool CProcessMutex: UnLock () 73 {74 int ret = sem_post (m_pSem ); 75 if (ret! = 0) 76 {77 return false; 78} 79 return true; 80} 81 82 # endif