corrected the completely wrong example (bug 545427)
git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@15702 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
This commit is contained in:
@@ -22,10 +22,10 @@ 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:
|
running and then waits until the main thread signals it to continue:
|
||||||
|
|
||||||
\begin{verbatim}
|
\begin{verbatim}
|
||||||
class MyWaitingThread : public wxThread
|
class MySignallingThread : public wxThread
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
MyWaitingThread(wxMutex *mutex, wxCondition *condition)
|
MySignallingThread(wxMutex *mutex, wxCondition *condition)
|
||||||
{
|
{
|
||||||
m_mutex = mutex;
|
m_mutex = mutex;
|
||||||
m_condition = condition;
|
m_condition = condition;
|
||||||
@@ -35,19 +35,20 @@ public:
|
|||||||
|
|
||||||
virtual ExitCode Entry()
|
virtual ExitCode Entry()
|
||||||
{
|
{
|
||||||
// wait for the signal from the main thread: it is absolutely necessary
|
|
||||||
// to look the mutex before doing it!
|
|
||||||
m_mutex->Lock();
|
|
||||||
m_condition->Signal();
|
|
||||||
m_mutex->Unlock();
|
|
||||||
|
|
||||||
... do our job ...
|
... 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;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
wxCondition *m_condition;
|
wxCondition *m_condition;
|
||||||
|
wxMutex *m_mutex;
|
||||||
};
|
};
|
||||||
|
|
||||||
int main()
|
int main()
|
||||||
@@ -55,25 +56,29 @@ int main()
|
|||||||
wxMutex mutex;
|
wxMutex mutex;
|
||||||
wxCondition condition(mutex);
|
wxCondition condition(mutex);
|
||||||
|
|
||||||
for ( int i = 0; i < 10; i++ )
|
// the mutex should be initially locked
|
||||||
{
|
mutex.Lock();
|
||||||
MyWaitingThread *thread = new MyWaitingThread(&mutex, &condition);
|
|
||||||
|
// 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();
|
thread->Run();
|
||||||
}
|
|
||||||
|
|
||||||
// wake up one of the threads
|
// wait for the thread termination: Wait() atomically unlocks the mutex
|
||||||
condition.Signal();
|
// which allows the thread to continue and starts waiting
|
||||||
|
condition.Wait();
|
||||||
// wake up all the other ones
|
|
||||||
condition.Broadcast();
|
|
||||||
|
|
||||||
... wait until they terminate or do something else ...
|
|
||||||
|
|
||||||
|
// now we can exit
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
\end{verbatim}
|
\end{verbatim}
|
||||||
|
|
||||||
|
Of course, here it would be much better to simply use a joinable thread and
|
||||||
|
call \helpref{wxThread::Wait}{wxthreadwait} on it, but this example does
|
||||||
|
illustrate the importance of properly locking the mutex when using
|
||||||
|
wxCondition.
|
||||||
|
|
||||||
\wxheading{Derived from}
|
\wxheading{Derived from}
|
||||||
|
|
||||||
None.
|
None.
|
||||||
|
Reference in New Issue
Block a user