Synchronization problem with wxCondition and wxMutex
Chris
chrismc911 at hotmail.com
Sun Feb 3 12:18:05 PST 2008
Hi,
I have a synchronization problem. I have extended a ListCtrl that
always checks for updates in a thread and updates the contents of the
ListCtrl. In the below code simplified code you can see
MappingListView::Start and MappingListView::Stop functions. I call
these when I want to pause the updating of the ListCtrl. The thread
itself is always running but waiting for a signal emitted in the
MappingListView::Start function.
Somehow the code deadlocks. The signal seems to get lost and the wait
not returning. I used mutexes as described in the documentation to
prevent signals to be lost. But I seem to do something wrong.
Any suggestions are highly welcome!
Thanks, Chris
You can find this simplified code also at: http://pastebin.com/m5fc1645c
wxCondition* waitForRunning;
wxCondition* waitForPaused;
wxMutex waitForRunningMutex;
wxMutex waitForPausedMutex;
volatile bool paused;
MappingListView::MappingListView (...)
{
waitForRunning = new wxCondition (waitForRunningMutex);
waitForPaused = new wxCondition (waitForPausedMutex);
waitForRunningMutex.Lock ();
waitForPausedMutex.Lock ();
paused = false;
wxThread::Create ();
}
void MappingListView::Start ()
{
if (paused) {
paused = false;
waitForRunningMutex.Lock ();
waitForRunning->Signal ();
waitForRunningMutex.Unlock ();
} else {
wxThread::Run();
}
}
void MappingListView::Stop ()
{
paused = true;
waitForPaused->Wait ();
}
wxThread::ExitCode MappingListView::Entry ()
{
while (! wxThread::TestDestroy ()) {
if (paused) {
waitForPausedMutex.Lock ();
waitForPaused->Signal ();
waitForPausedMutex.Unlock ();
waitForRunning->Wait ();
}
if (wxThread::TestDestroy ()) break;
//
// DO PERIODIC THREAD WORK HERE
//
}
return 0;
}
More information about the wx-users
mailing list