Skip to content

Instantly share code, notes, and snippets.

@vadz
Last active August 29, 2015 14:00
Show Gist options
  • Save vadz/176dbe919f4839581ce3 to your computer and use it in GitHub Desktop.
Save vadz/176dbe919f4839581ce3 to your computer and use it in GitHub Desktop.
Thread safe (?) way to wait for the main thread
class Thread : public wxThread {
int Entry() override {
for (;;) {
{
lock_guard<mutex> lock(m_mutex);
if (!m_window)
return 0;
m_window->CallAfter(&Window::ProvideResult);
}
lock_guard<mutex> lock(m_mutexCond);
while (m_window && !m_window->m_result)
m_cond.wait(m_mutexCond);
if (!m_window)
return 0;
// ... use m_result ...
}
}
mutex m_mutex, m_mutexCond;
condition m_cond;
Window* m_window;
};
class Window : public wxWindow {
~Window() {
lock_guard<mutex> lock(m_thread->m_mutex);
m_thread->m_window = nullptr;
m_thread->m_cond.notify_one();
}
void ProvideResult() {
m_result = true;
m_thread->m_cond.notify_one();
}
Thread* m_thread;
bool m_result = false;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment