Contents Up Previous Next 原文へのリンク


wxCondition 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 to get it you must use a mutex together with the condition variable.


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
    MySignallingThread(wxMutex *mutex, wxCondition *condition)
        m_mutex = mutex;
        m_condition = condition;


    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;

    wxCondition *m_condition;
    wxMutex *m_mutex;

int main()
    wxMutex mutex;
    wxCondition condition(mutex);

    // the mutex should be initially locked

    // 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);


    // wait for the thread termination: Wait() atomically unlocks the mutex
    // which allows the thread to continue and starts waiting

    // 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.

Derived from


Include files


See also

wxThread, wxMutex




wxCondition(wxMutex& mutex)

Default and only constructor. The mutex must be locked by the caller before calling Wait function.



Destroys the wxCondition object. The destructor is not virtual so this class should not be used polymorphically.


void 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



void 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



void Wait()

Waits until the condition is signalled.

bool Wait(unsigned long sec, unsigned long nsec)

Waits until the condition is signalled or the timeout has elapsed.

Note that the mutex associated with this condition must be acquired by the thread before calling this method.




Return value

The second form returns TRUE if the condition has been signalled, or FALSE if it returned because the timeout has elapsed.