Mutex is a kind of a synchronization object that provides MUTually EXclusive (MUTEX) access to some part of the code (in some systems this is also known as Resource). Mutex can be locked by a Thread using the Mutex_lock method, then released. Only one thread at a time owns the lock on any given Mutex when the lock is released, the next thread (if any) waiting for the Mutex is activated.
If a thread tries to lock a Mutex that is already locked, the thread will be set to wait until a timeout expires or until the Mutex object is unlocked. Take care to prevent deadlocks: for example, when one thread owns Mutex m1 and tries to lock Mutex m2,while a second thread already owns Mutex m2 and is trying to lock Mutex m1. Deadlocks can occur not only with Mutexes, but practically with any set of synchronization objects. Since the OS can not yet detect all deadlocks, it is up to programmers to design their systems accordingly.
It doesn't matter how long you've locked a Mutex object-it unlocks the first time you call Mutex_unlock(). The system does not allow unowned Mutex objects to be unlocked.
Constructs the named Mutex object.
#include <cywin.h> ... struct Mutex data_mutex; ... Mutex_ctor( &data_mutex, "shared_data" ); ... if( Mutex_lock( &data_mutex, 1000 ) ) { // Uses shared data. ... Mutex_unlock( &data_mutex ); } else { TRACE( "Timeout while trying to access." ); } ... Mutex_dtor( &data_mutex, LEAVE_MEMORY ); ...
Destructor.
#include <cywin.h> ... struct Mutex data_mutex; ... Mutex_ctor( &data_mutex, "shared_data" ); ... if( Mutex_lock( &data_mutex, 1000 ) ) { // Uses shared data. ... Mutex_unlock( &data_mutex ); } else { TRACE( "Timeout while trying to access." ); } ... Mutex_dtor( &data_mutex, LEAVE_MEMORY ); ...
Checks whether the Mutex is locked.
#include <cywin.h> ... struct module_t main_module; struct Mutex data_mutex; ... Mutex_ctor( &data_mutex, "shared_data" ); ... while( Mutex_is_locked( &data_mutex ) ) { sleep( 250 ); } Mutex_lock( &data_mutex, 0 ); ... // Uses shared data. Mutex_unlock( &data_mutex ); ... Mutex_dtor( &data_mutex, LEAVE_MEMORY ); ...
Returns a lock.
Attempts to get the lock for specified time. Note that if you already locked an object, another Mutex_lock() operations does absolutely nothing, and the first Mutex_unlock() call unlocks it immediately no matter how many times you've called the Mutex_lock() method. Be careful when designing your Mutex logic!
#include <cywin.h> ... struct Mutex data_mutex; ... Mutex_ctor( &data_mutex, "shared_data" ); ... if( Mutex_lock( &data_mutex, 1000 ) ) { // Uses shared data. ... Mutex_unlock( &data_mutex ); } else { TRACE( "Timeout while trying to access." ); } ... Mutex_dtor( &data_mutex, LEAVE_MEMORY ); ...
Unlocks a Mutex object.
Note that if you did not Mutex_lock() it before, the system ignores the unlock command and prints a warning on the console. Also be warned: Mutex_unlock() releases the lock immediately. No matter how many times Mutex_lock() was called, only the first Mutex_unlock() works. any repeared commands will be ignored.
#include <cywin.h> ... struct Mutex data_mutex; ... Mutex_ctor( &data_mutex, "shared_data" ); ... if( Mutex_lock( &data_mutex, 1000 ) ) { // Uses shared data. ... Mutex_unlock( &data_mutex ); } else { TRACE( "Timeout while trying to access." ); } ... Mutex_dtor( &data_mutex, LEAVE_MEMORY ); ...