Reprinted: http://blog.csdn.net/webols/article/details/6820028
Differences between mutex critical section critical sessions in synchronization control
Win32 mutex is very similar to critical seciton. But He Increases elasticity at a sacrifice speed. Mutex is the core object
Differences between mutex and critical section:
- Locking an unowned mutex takes almost 100 times more time than locking an unowned critical section.
- Mutex can be used across processes. The critical section can only be used in one process.
- Wait for a mutex. You can specify the duration of the "end wait. Critical section does not work.
Comparison of related functions of the two objects:
Critical_section mutex core object
Initializecriticalsection () createmutex (), openmutex ()
Entercriticalsection () waitforsingleobject (), waitformultipleobject (), msgwaitformultipleobject ()
Leavecriticalsection () releasemutex ()
Deletecriticalsection () closehandle ()
In order to use mutex across processes, the name should be specified when mutex is generated. Then other processes of the system can use the name to process the mutex.
Basic knowledge:
Generate a mutex
Creates or opens a named or unnamed mutex object. to specify an access mask for the object, use the createmutexex function.
Handle winapi createmutex (
_ In lpsecurity_attributes lpmutexattributes, // Security Attribute, null table default
_ In bool binitialowner, // if you want the thread that "calls createmutex" to own the generated mutex, set this value to true.
_ In lpctstr lpname // mutex name
);
Return Value: if the operation succeeds, a handle is returned. Otherwise, null is returned. Call getlasterror to obtain further information. If the specified mutex name already exists, getlasterror () will return error_already_exists.
When mutex is not required, closehandle is called to disable it.
Open a mutex)
Any process or thread can open an existing mutex by name.
If you call createmutex and specify an existing mutex name. Win32 returns a mutex handle. Instead of generating a mutex, getlasterror will return error_already_exists
Lock a mutex)
To obtain the ownership of mutex, use wait... Series functions. Wait .. () What mutex does is similar to what entercriticalsection does for criticalsection. Once no thread has mutex, it is in the excitation state and wait... will succeed.
Release a mutex)
The thread that obtains the mutex calls releasemutex and releases it so that the mutex is in the excitation state.
Releases ownership of the specified mutex object.
Bool winapi releasemutex (
_ In handle hmutex // handle of mutex to be released
);
Return Value: true for success, false for failure
Process discarded mutex)
If a thread has a mutex and does not call releasemutex before the end, the mutex will not be destroyed and will be considered as "not owned" and "not fired ", the thread in the next wait will be notified with wait_abandoned_0, whether the thread should end with exitthread or end with dropping.
If other threads are waiting for this mutex with waitformultipleobjects (), the function will also return. The returned value is between wait_abandoned_0 and wait_abandoned_0_n + 1, where N refers to the number of elements in the handle array. The thread can use this to understand that the mutex has been abandoned. For waitforsingleobject, only wait_abandoned_0 is returned.
Why is there an original owner?
The second parameter of createmutex allows you to specify whether the current thread has the mutex to be generated immediately to prevent the occurrence of race condition.
Refer to Win32 Multi-Thread Programming