wxmutexa mutex object is a synchronization object whose state is set to signaled when it is not owned by any thread, and nonsignaled when it is owned. its name comes from its usefulness in coordinating mutually-exclusive access to a shared resource as only one thread at a time can own a mutex object. mutexes may be recursive in the sense that a thread can lock a mutex which it had already locked before (instead of dead locking the entire process in this situation by starting to wait on a mutex which will never be released while the thread is waiting) but using them is not recommended and they are not recursive by default. the reason for this is that recursive mutexes are not supported by all unix flavours and, worse, they cannot be used with wxcondition. for example, when several threads use the data stored in the linked list, modifications to the list should only be allowed to one thread at a time because during a new node addition the list integrity is temporarily broken (this is also called program invariant). example
notice how wxmutexlocker was used in the second function to ensure that the mutex is unlocked in any case: whether the function returns true or false (because the destructor of the local object lock is always called). using this class instead of directly using wxmutex is, in general safer and is even more so if your program uses c++ exceptions. constants
enum wxmutextype { // normal mutex: try to always use this one wxmutex_default, // recursive mutex: don't use these ones with wxcondition wxmutex_recursive };derived from none. include files <wx/thread.h> see also wxthread, wxcondition, wxmutexlocker, wxcriticalsection members
wxmutex::wxmutex
wxmutex::wxmutexwxmutex(wxmutextype type = wxmutex_default) default constructor.
wxmutex::~wxmutex~wxmutex() destroys the wxmutex object.
wxmutex::lockwxmutexerror lock() locks the mutex object. return value one of:
wxmutex::trylockwxmutexerror trylock() tries to lock the mutex object. if it can't, returns immediately with an error. return value one of:
wxmutex::unlockwxmutexerror unlock() unlocks the mutex object. return value one of:
|