浏览代码

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 年之前
父节点
当前提交
c2f4991e99
共有 1 个文件被更改,包括 11 次插入6 次删除
  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)
 {
-// 	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)