wxconditionwxcondition variables correspond to pthread conditions or to win32 event objects. they may be used in a multithreaded application to wait until the given condition becomes true which happens when the condition becomes signaled. for example, if a worker thread is doing some long task and another thread has to wait until it is finished, the latter thread will wait on the condition object and the worker thread will signal it on exit (this example is not perfect because in this particular case it would be much better to just wait() for the worker thread, but if there are several worker threads it already makes much more sense). note that a call to signal() may happen before the other thread calls wait() and, just as with the pthread conditions, the signal is then lost and so if you want to be sure that you don't miss it you must keep the mutex associated with the condition initially locked and lock it again before calling signal(). of course, this means that this call is going to block until wait() is called by another thread. example this example shows how a main thread may launch a worker thread which starts running and then waits until the main thread signals it to continue:
class mysignallingthread : public wxthread { public: mysignallingthread(wxmutex *mutex, wxcondition *condition) { m_mutex = mutex; m_condition = condition; create(); } virtual exitcode entry() { ... do our job ... // tell the other(s) thread(s) that we're about to terminate: we must // lock the mutex first or we might signal the condition before the // waiting threads start waiting on it! wxmutexlocker lock(m_mutex); m_condition.broadcast(); // same as signal() here -- one waiter only return 0; } private: wxcondition *m_condition; wxmutex *m_mutex; }; int main() { wxmutex mutex; wxcondition condition(mutex); // the mutex should be initially locked mutex.lock(); // create and run the thread but notice that it won't be able to // exit (and signal its exit) before we unlock the mutex below mysignallingthread *thread = new mysignallingthread(&mutex, &condition); thread->run(); // wait for the thread termination: wait() atomically unlocks the mutex // which allows the thread to continue and starts waiting condition.wait(); // now we can exit return 0; }of course, here it would be much better to simply use a joinable thread and call wxthread::wait on it, but this example does illustrate the importance of properly locking the mutex when using wxcondition. constants the following return codes are returned by wxcondition member functions:
enum wxconderror { wxcond_no_error = 0, // successful completion wxcond_invalid, // object hasn't been initialized successfully wxcond_timeout, // waittimeout() has timed out wxcond_misc_error // some other error };derived from none. include files <wx/thread.h> see also members
wxcondition::wxcondition
wxcondition::wxconditionwxcondition(wxmutex& mutex) default and only constructor. the mutex must be locked by the caller before calling wait function. use isok to check if the object was successfully initialized.
wxcondition::~wxcondition~wxcondition() destroys the wxcondition object. the destructor is not virtual so this class should not be used polymorphically.
wxcondition::broadcastvoid broadcast() broadcasts to all waiting threads, waking all of them up. note that this method may be called whether the mutex associated with this condition is locked or not. see also
wxcondition::isokbool isok() const returns true if the object had been initialized successfully, false if an error occurred.
wxcondition::signalvoid signal() signals the object waking up at most one thread. if several threads are waiting on the same condition, the exact thread which is woken up is undefined. if no threads are waiting, the signal is lost and the condition would have to be signalled again to wake up any thread which may start waiting on it later. note that this method may be called whether the mutex associated with this condition is locked or not. see also
wxcondition::waitwxconderror wait() waits until the condition is signalled. this method atomically releases the lock on the mutex associated with this condition (this is why it must be locked prior to calling wait) and puts the thread to sleep until signal or broadcast is called. it then locks the mutex again and returns. note that even if signal had been called before wait without waking up any thread, the thread would still wait for another one and so it is important to ensure that the condition will be signalled after wait or the thread may sleep forever. return value returns wxcond_no_error on success, another value if an error occurred. see also
wxcondition::waittimeoutwxconderror waittimeout(unsigned long milliseconds) waits until the condition is signalled or the timeout has elapsed. this method is identical to wait except that it returns, with the return code of wxcond_timeout as soon as the given timeout expires. parameters milliseconds
return value returns wxcond_no_error if the condition was signalled, wxcond_timeout if the timeout elapsed before this happened or another error code from wxconderror enum.
|