$szTitle = "wxmutex"; include "./_header.inc"; ?>
a 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
// this variable has an "s_" prefix because it is static: seeing an "s_" in
// a multithreaded program is in general a good sign that you should use a
// mutex (or a critical section)
static wxmutex *s_mutexprotectingtheglobaldata;
// we store some numbers in this global array which is presumably used by
// several threads simultaneously
wxarrayint s_data;
void mythread::addnewnode(int num)
{
// ensure that no other thread accesses the list
s_mutexprotectingthegloballist->lock();
s_data.add(num);
s_mutexprotectingthegloballist->unlock();
}
// return true the given number is greater than all array elements
bool mythread::isgreater(int num)
{
// before using the list we must acquire the mutex
wxmutexlocker lock(s_mutexprotectingtheglobaldata);
size_t count = s_data.count();
for ( size_t n = 0; n < count; n++ )
{
if ( s_data[n] > num )
return false;
}
return true;
}
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::~wxmutex
wxmutex::lock
wxmutex::trylock
wxmutex::unlock
wxmutex(wxmutextype type = wxmutex_default)
default constructor.
~wxmutex()
destroys the wxmutex object.
wxmutexerror lock()
locks the mutex object.
return value
one of:
wxmutex_no_error | there was no error. |
wxmutex_dead_lock | a deadlock situation was detected. |
wxmutexerror trylock()
tries to lock the mutex object. if it can't, returns immediately with an error.
return value
one of:
wxmutex_no_error | there was no error. |
wxmutex_busy | the mutex is already locked by another thread. |
wxmutexerror unlock()
unlocks the mutex object.
return value
one of:
wxmutex_no_error | there was no error. |
wxmutex_unlocked | the calling thread doesn't own the mutex. |
include "./_footer.inc"; ?>