Skip to content

Commit

Permalink
{auto} fixed race condition in platform. It was possible that the wor…
Browse files Browse the repository at this point in the history
…ker thread whould not be notified when we stopped the platform.
  • Loading branch information
Aljar committed Aug 24, 2018
1 parent c996acd commit 866c8fc
Showing 1 changed file with 12 additions and 0 deletions.
12 changes: 12 additions & 0 deletions platform.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::mutex> 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();
Expand Down

0 comments on commit 866c8fc

Please sign in to comment.