Bläddra i källkod

Quick shutdown of ConsoleHandler thread

Ivan Savenko 7 månader sedan
förälder
incheckning
e73d05933d
2 ändrade filer med 6 tillägg och 2 borttagningar
  1. 4 2
      lib/CConsoleHandler.cpp
  2. 2 0
      lib/CConsoleHandler.h

+ 4 - 2
lib/CConsoleHandler.cpp

@@ -251,8 +251,9 @@ int CConsoleHandler::run()
 				if ( cb )
 					cb(buffer, false);
 		}
-		else
-			std::this_thread::sleep_for(std::chrono::milliseconds(100));
+
+		std::unique_lock guard(shutdownMutex);
+		shutdownVariable.wait_for(guard, std::chrono::seconds(1));
 
 		if (shutdownPending)
 			return -1;
@@ -308,6 +309,7 @@ void CConsoleHandler::end()
 	{
 #ifndef _MSC_VER
 		shutdownPending = true;
+		shutdownVariable.notify_all();
 #else
 		TerminateThread(thread.native_handle(),0);
 #endif

+ 2 - 0
lib/CConsoleHandler.h

@@ -94,6 +94,8 @@ private:
 	//function to be called when message is received - string: message, bool: whether call was made from in-game console
 	std::function<void(const std::string &, bool)> cb;
 
+	std::condition_variable shutdownVariable;
+	std::mutex shutdownMutex;
 	std::atomic<bool> shutdownPending = false;
 
 	std::mutex smx;