WxThread

From WxWiki

Jump to: navigation, search

Contents

[edit] Prefer Entry() to the Constructor

If you have problems using something in the thread that you create in the constructor of the wxThread-derived class, move it to the Entry() function. That is much safer.

[edit] wxApp::Yield()

Many of the general applications of threads can also be implemented with wxApp::Yield(). This is, however, to be avoided, since in some cases this would cause an event loop to be recursively entered, which is a Bad Thing and hard to debug.

[edit] Problems Deleting a Thread

If you have

OnClose(...) {
  thread->Delete();
  Destroy();
}

Delete(), while waiting for the thread to destroy itself, processes messages. Since the window has not yet been destroyed while doing this, OnClose() is called *again* while waiting, which calls Delete() *again*. Once this recursive delete is complete, the first delete will hang, fail, or both. I worked around it by instead doing:

OnClose(...) {
  if (m_destroying) return;
  m_destroying++;                                                                                                                           
  thread->Delete();
  Destroy():
}

There is, however, some discussion about why OnClose() should be called again, and thus this shouldn't be needed.

[edit] General Tips When Debugging

Make sure that you specify wxTHREAD_JOINABLE when you need it. Not doing so will let the default constructor use wxTHREAD_DETACHED and result in mysterious access violations when deleting the thread which are likely to make you go mad.

[edit] Official Documentation

See wxThread.

See also the Thread Overview in the online reference documentation.

Personal tools