Dynare++: fix bug in new threading code

At the end of a thread, we must first notify the main thread waiting on the
condition variable, then unlock the mutex. We must do these two operations in
that order, otherwise there is a possibility of having the main process
destroying the condition variable before the thread tries to notify it (if all
other threads terminate at the same time and bring the counter down to zero).
For that reason, we cannot use std::notify_all_at_thread_exit().

Bug introduced in commit 752a02a36.
time-shift
Sébastien Villemot 2019-02-01 23:36:35 +01:00
parent 44a378bf26
commit 3e5b4084b8
No known key found for this signature in database
GPG Key ID: 2CECE9350ECEBE4A
1 changed files with 9 additions and 1 deletions

View File

@ -28,7 +28,15 @@ namespace sthread
(*it)->operator()(mut_threads);
std::unique_lock<std::mutex> lk2{mut_cv};
counter--;
std::notify_all_at_thread_exit(cv, std::move(lk2));
/* First notify the thread waiting on the condition variable, then
unlock the mutex. We must do these two operations in that order,
otherwise there is a possibility of having the main process
destroying the condition variable before the thread tries to
notify it (if all other threads terminate at the same time and
bring the counter down to zero).
For that reason, we cannot use std::notify_all_at_thread_exit() */
cv.notify_one();
lk2.unlock();
}};
th.detach();
++it;