Mutex object 1. Create a handle type variable handle hmutex;
2. hmutex = createmutex (null, true, "ticket"); createmutex creates a mutex object
(Call releasemutex (hmutex) in time after successful creation to reduce the count by one .)
Iii. waitforsingleobject (hmutex, infinite); use this function where synchronization is needed.
4. releasemutex (hmutex); use this function to release the mutex lock after a waiting object is used. example # include <windows. h> # include <iostream. h> DWORD winapi fun1proc (lpvoidlpparameter // thread data); DWORD winapi fun2proc (lpvoid lpparameter/thread data); int ticket = 100; handle hmutex; void main () {handle handle1; handle handle2; handle1 = createthread (null, 0, fun1proc, null, 0, null); handle2 = createthread (null, 0, fun2proc, null, 0, null); closehandle (handl E1); closehandle (handle2); hmutex = createmutex (null, true, "ticket"); If (hmutex) {If (error_already_exists = getlasterror ()) {cout <"thereis already have a instance runing !!! "<Endl; return ;}/// waitforsingleobject (hmutex, infinite); // releasemutex (hmutex); sleep (4000 );} DWORD winapi fun1proc (lpvoid lpparameter // thread data) {While (true) {waitforsingleobject (hmutex, infinite); If (ticket> 0) {sleep (1 ); cout <"thisis fun1proc succeeded" <ticket -- <Endl;} else {break;} releasemutex (hmutex);} waitforsingleobject (hmutex, infinite ); cout <"fun1procrunning" <Endl; releasemutex (hmutex); Return 0;} DWORD winapi fun2proc (lpvoid lpparameter // thread data) {While (true) {waitforsingleobject (hmutex, infinite); If (ticket> 0) {sleep (1); cout <"thisis fun2proc restart" <ticket -- <Endl;} else {break ;} releasemutex (hmutex);} waitforsingleobject (hmutex, infinite); cout <"fun2procrunning" <Endl; releasemutex (hmutex); Return 0 ;}