1
0
mirror of https://github.com/vcmi/vcmi.git synced 2024-12-24 22:14:36 +02:00

Replaced barrier with mutex due to data races:

Destruction of barrier while one of the threads is still in barrier.wait() is illegal. This may happen if caller thread reaches wait() after helper thread and immediately return's from the function destroying barrier which is still in use by helper thread
This commit is contained in:
Ivan Savenko 2015-12-04 00:12:49 +02:00
parent f02e553b70
commit c2f4991e99

View File

@ -2673,19 +2673,24 @@ void VCAI::finish()
void VCAI::requestActionASAP(std::function<void()> whatToDo)
{
// static boost::mutex m;
// boost::unique_lock<boost::mutex> mylock(m);
boost::mutex mutex;
mutex.lock();
boost::barrier b(2);
boost::thread newThread([&b,this,whatToDo]()
boost::thread newThread([&mutex,this,whatToDo]()
{
setThreadName("VCAI::requestActionASAP::helper");
SET_GLOBAL_STATE(this);
boost::shared_lock<boost::shared_mutex> gsLock(cb->getGsMutex());
b.wait();
// unlock mutex and allow parent function to exit
mutex.unlock();
whatToDo();
});
b.wait();
// wait for mutex to unlock and for thread to initialize properly
mutex.lock();
// unlock mutex - boost dislikes destruction of locked mutexes
mutex.unlock();
}
void VCAI::lostHero(HeroPtr h)