MT#62181 fix join race condition

In some cases it can happen that ::join() is called from multiple
threads at the same time. Checking the ::joinable() flag is not
race-free and so is unreliable to make sure that only one other thread
attempts to join at any given time. Add a mutex and a state variable to
make sure only one thread attempts to join, and make any other threads
wait.

Change-Id: I02fd236a416b035c98642535f1521be4a3a63fd9
mr13.3.1
Richard Fuchs 10 months ago
parent 805aad91d2
commit fb187929dd

@ -82,9 +82,18 @@ void AmThread::stop()
void AmThread::join()
{
// only when neither stopped nor joined
if (_td.joinable())
_td.join();
// don't attempt to join thread that doesn't exist
if (_state == state::idle)
return;
// make sure only one other thread joins this one. all others
// are made to wait through the mutex
std::lock_guard<std::mutex> _l(_join_mt);
if (!_joined) {
if (_td.joinable())
_td.join();
_joined = true;
}
}

@ -122,6 +122,9 @@ class AmThread
std::atomic<state> _state;
std::mutex _join_mt;
bool _joined;
void _start();
protected:
@ -135,7 +138,8 @@ public:
unsigned long _pid;
AmThread()
: _state(state::idle)
: _state(state::idle),
_joined(false)
{}
virtual ~AmThread();

Loading…
Cancel
Save