Ticket #11501 (closed defect: fixed)
Fixed deadlock with pthread-implemented wxThread deletion.
| Reported by: | talon_karrde | Owned by: | |
|---|---|---|---|
| Priority: | normal | Milestone: | |
| Component: | base | Version: | 2.9-svn |
| Keywords: | wxThread deadlock pthread unix | Cc: | |
| Blocked By: | Patch: | yes | |
| Blocking: |
Description
A deadlock occurs if one wxThread waits for another wxThread to finish in its destructor. Here's a sample code which demonstrates that:
class WorkerThread : public wxThread
{
public:
WorkerThread()
{
this->pHelperThread = new HelperThread(this);
this->pHelperThread->Create();
this->pHelperThread->Run();
}
~WorkerThread()
{
{
wxCriticalSectionLocker cs(this->HelperThreadCS);
if(this->pHelperThread) this->pHelperThread->Delete();
}
for(;;)
{
{
wxCriticalSectionLocker cs(this->HelperThreadCS);
if(this->pHelperThread == NULL) break;
}
wxThread::Yield();
}
}
wxThread *pHelperThread;
wxCriticalSection HelperThreadCS;
};
class HelperThread : public wxThread
{
public:
HelperThread(WorkerThread *pParentThread) { this->pParentThread =
pParentThread; }
~HelperThread()
{
wxCriticalSectionLocker cs(pParentThread->HelperThreadCS);
pParentThread->pHelperThread = NULL;
}
WorkerThread *pParentThread;
};
If WorkerThread finishes before HelperThread it attempts to delete it in its dtor and then wait for it to finish. WorkerThread's dtor, however, is executed while gs_mutexDeleteThread is locked, and HelperThread must acquire this mutex in order to clean up before exiting, which results in a deadlock.
I've attached a patch which simply calls the deleted wxThread's dtor before acquiring the mutex.
Attachments
Change History
Note: See
TracTickets for help on using
tickets.

