Skip to content

Commit 866c8fc

Browse files
committed
{auto} fixed race condition in platform. It was possible that the worker thread whould not be notified when we stopped the platform.
1 parent c996acd commit 866c8fc

File tree

1 file changed

+12
-0
lines changed

1 file changed

+12
-0
lines changed

platform.cpp

+12
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,20 @@ Platform::~Platform()
6363
*/
6464
void Platform::stop()
6565
{
66+
// we modify the _running, it may be that the other thread has a lock
67+
// to protect this variable, although it is attomic, we need to respect
68+
// this lock. E.g. run uses a lock before it enters the _condition.wait()
69+
// if we don't take a lock here, we may update running and call
70+
// notify before the other thread called wait, but already checked if
71+
// we were running. In that case the other thread will never be notified.
72+
std::unique_lock<std::mutex> lock(_mutex);
73+
6674
// we set it to false, but if the old value was not true then we leap out
6775
if (!_running.exchange(false)) return;
76+
77+
// we need to release the lock over here, so the other thread can move
78+
// on.
79+
lock.unlock();
6880

6981
// signal the thread in case it is waiting for input
7082
_condition.notify_one();

0 commit comments

Comments
 (0)