Przeglądaj źródła

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
Ivan Savenko 10 lat temu
rodzic
commit
c2f4991e99
1 zmienionych plików z 11 dodań i 6 usunięć
  1. 11 6
      AI/VCAI/VCAI.cpp

+ 11 - 6
AI/VCAI/VCAI.cpp

@@ -2673,19 +2673,24 @@ void VCAI::finish()
 
 
 void VCAI::requestActionASAP(std::function<void()> whatToDo)
 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");
 		setThreadName("VCAI::requestActionASAP::helper");
 		SET_GLOBAL_STATE(this);
 		SET_GLOBAL_STATE(this);
 		boost::shared_lock<boost::shared_mutex> gsLock(cb->getGsMutex());
 		boost::shared_lock<boost::shared_mutex> gsLock(cb->getGsMutex());
-		b.wait();
+		// unlock mutex and allow parent function to exit
+		mutex.unlock();
 		whatToDo();
 		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)
 void VCAI::lostHero(HeroPtr h)