From a143f3d408fc0a72b5d45d5fd56f35e985f4c86a Mon Sep 17 00:00:00 2001 From: Kevin Wojniak Date: Sun, 19 Jun 2016 17:33:28 -0700 Subject: [PATCH] Fix spurious wakeup causing thread to exit early on 32-bit Windows --- examples/cpp-multithread/main.cpp | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/examples/cpp-multithread/main.cpp b/examples/cpp-multithread/main.cpp index 1464df2d..2b30f54e 100644 --- a/examples/cpp-multithread/main.cpp +++ b/examples/cpp-multithread/main.cpp @@ -12,8 +12,8 @@ using namespace std; uiMultilineEntry *e; condition_variable cv; mutex m; -unique_lock ourlock(m); thread *timeThread; +bool quitting = false; void sayTime(void *data) { @@ -25,12 +25,16 @@ void sayTime(void *data) void threadproc(void) { - ourlock.lock(); - while (cv.wait_for(ourlock, chrono::seconds(1)) == cv_status::timeout) { + while (!quitting) { time_t t; char *base; char *s; + { + unique_lock ourlock(m); + (void)cv.wait_for(ourlock, chrono::seconds(1)); + } + t = time(NULL); base = ctime(&t); s = new char[strlen(base) + 1]; @@ -41,6 +45,7 @@ void threadproc(void) int onClosing(uiWindow *w, void *data) { + quitting = true; cv.notify_all(); // C++ throws a hissy fit if you don't do this // we might as well, to ensure no uiQueueMain() gets in after uiQuit() @@ -82,7 +87,6 @@ int main(void) uiBoxAppend(b, uiControl(e), 1); // timeThread needs to lock ourlock itself - see http://stackoverflow.com/a/34121629/3408572 - ourlock.unlock(); timeThread = new thread(threadproc); uiWindowOnClosing(w, onClosing, NULL);