From 866c8fc31f89e7f419946758be887b870bb69971 Mon Sep 17 00:00:00 2001 From: Aljar Meesters Date: Fri, 24 Aug 2018 13:24:06 +0200 Subject: [PATCH] {auto} fixed race condition in platform. It was possible that the worker thread whould not be notified when we stopped the platform. --- platform.cpp | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/platform.cpp b/platform.cpp index ea9f89d..60b1df1 100644 --- a/platform.cpp +++ b/platform.cpp @@ -63,8 +63,20 @@ Platform::~Platform() */ void Platform::stop() { + // we modify the _running, it may be that the other thread has a lock + // to protect this variable, although it is attomic, we need to respect + // this lock. E.g. run uses a lock before it enters the _condition.wait() + // if we don't take a lock here, we may update running and call + // notify before the other thread called wait, but already checked if + // we were running. In that case the other thread will never be notified. + std::unique_lock lock(_mutex); + // we set it to false, but if the old value was not true then we leap out if (!_running.exchange(false)) return; + + // we need to release the lock over here, so the other thread can move + // on. + lock.unlock(); // signal the thread in case it is waiting for input _condition.notify_one();