Ver código fonte

Prevent lock order inversion

Paul-Louis Ageneau 4 anos atrás
pai
commit
ab7d7fefe0
2 arquivos alterados com 4 adições e 3 exclusões
  1. 3 2
      src/threadpool.cpp
  2. 1 1
      src/threadpool.hpp

+ 3 - 2
src/threadpool.cpp

@@ -43,8 +43,7 @@ int ThreadPool::count() const {
 }
 
 void ThreadPool::spawn(int count) {
-	std::scoped_lock lock(mMutex, mWorkersMutex);
-	mJoining = false;
+	std::unique_lock lock(mWorkersMutex);
 	while (count-- > 0)
 		mWorkers.emplace_back(std::bind(&ThreadPool::run, this));
 }
@@ -62,6 +61,8 @@ void ThreadPool::join() {
 		w.join();
 
 	mWorkers.clear();
+
+	mJoining = false;
 }
 
 void ThreadPool::run() {

+ 1 - 1
src/threadpool.hpp

@@ -73,7 +73,7 @@ protected:
 
 	std::vector<std::thread> mWorkers;
 	int mWaitingWorkers = 0;
-	bool mJoining = false;
+	std::atomic<bool> mJoining = false;
 
 	struct Task {
 		clock::time_point time;