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:
|