multi-threaded synchronization mutex object
Author: Vpoet
Mail:[email protected]
The
use of critical sections is described in the http://blog.csdn.net/u013018721/article/details/46637215 articleOn the
issue of selling tickets between thread synchronization, this article will be based on the above, using mutually exclusive objects to synchronize threads.
first look at the Windows API
This function creates a named or unnamed mutex object
Lpmutexattributes
Pointer to a security_attributes structure, determines whether the returned handle can be Inherite D by the child processes. Iflpmutexattributes is NULL, the handle cannot be inherited.
The
parameter lpmutexattributes is the security property of the mutex object, and if NULL, the default security attribute is used
Binitialowner
[in] specifies the initial owner of the mutex object. If This value is TRUE and the caller created the mutex, the calling thread obtains ownership of the mutex object. Otherwise, the calling thread does not obtain ownership of the mutex. To determine if the caller created the mutex, see the Return Values section.
The
parameter Binitialowner Specifies the owner of the mutex and, if true, indicates that the thread that created the mutex has ownership of it, whereas conversely, the thread that created the mutex cannot take ownership of the object
Lpname
Pointer to a null-terminated string specifying the name of the mutex object. The name is limited to MAX_PATH characters. Name comparison is case sensitive.
If lpname matches the name of a existing named mutex object, this function requests mutex_all_access ACCESS to T He existing object. In this case, thebinitialowner parameter is ignored because it had already been set by the creating process. If the lpmutexattributes parameter is isn't NULL, it determines whether the handle can be inherited and its Securi Ty-descriptor member is ignored.
If lpname is NULL, the mutex object is created without a name.
If lpname matches the name of an existing event, Semaphore, waitable timer, job, or File-mapping object, the Func tion fails and the GetLastError function returns ERROR_INVALID_HANDLE. This occurs because these objects share the same name space.
Terminal services:the name can has a "global\" or "local\" prefix to explicitly create the object in the Global or Sessi On name space. The remainder of the name can contain any character except the backslash character (\). For more information, see Kernel Object Name Spaces.
Parameter lpname Specifies whether to be a named mutex or an anonymous mutex, the difference between them we leave behind to learn
This function releases ownership of the mutex, which means that the function has a signaled state for the mutex object
The parameter of the function is the handle object returned by the CreateMutex function
when using mutex events for thread synchronization, we also need to use a function WaitForSingleObject the function will wait for the mutex to have a state before returning otherwise it is blocked.
Next we'll take a look at it, notice that in the CreateMutex function we set the second argument to False, Because we need to create the mutex in the main thread, but put this
The ownership of the mutex object needs to be rotated to two sub-threads alternately sold.
code is as follows:
#include <windows.h> #include <stdio.h>static int number=10; HANDLE Mutex;dword WINAPI threadone (LPVOID lpparameter) {while (1) {WaitForSingleObject (mutex,infinite); if (number>0) {printf ("Window 1 sold%d tickets ... \ n", number); number--; Sleep (1000);} ReleaseMutex (Mutex);} return 0;} DWORD WINAPI threadtwo (LPVOID lpparameter) {while (1) {WaitForSingleObject (Mutex,infinite), if (number>0) {printf (" Window 2 sold%d tickets ... \ n ", number); Sleep (+); number--;} ReleaseMutex (Mutex);} return 0;} int main () {HANDLE hone,htwo; Mutex=createmutex (null,false,null);p rintf ("***********************vpoet******************\n"); Hone=createthread (null,0,threadone,null,0,null);p rintf ("Window 1 ticket opening: \ n"); Htwo=createthread (null,0,threadtwo,null,0,null);p rintf ("Window 2 ticket opening: \ n"); CloseHandle (HOne); CloseHandle (Htwo); while (TRUE) {if (number==0) {printf ("Sorry, the ticket is sold out!\n"); CloseHandle (Mutex); return 0;} Else{continue;}} return 0;}
Operation Result:
Well, no problem, we've reached our request.
Ok, next time we'll learn to work together. Use event objects for inter-thread synchronization.
Multi-threaded synchronization mutex object