From 409b31423533ac3e648f882e4d2380dcf4fcc3ec Mon Sep 17 00:00:00 2001 From: Thorbjørn Lindeijer Date: Tue, 2 May 2023 16:58:56 +0200 Subject: Avoid potential undefined behavior related to data race Using atomic operations to avoid a compiler potentially optimizing away the thread responsible for clean shutdown. --- src/account-server/main-account.cpp | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) (limited to 'src') diff --git a/src/account-server/main-account.cpp b/src/account-server/main-account.cpp index 4d5bf02a..65087ab5 100644 --- a/src/account-server/main-account.cpp +++ b/src/account-server/main-account.cpp @@ -46,6 +46,7 @@ #include "utils/stringfilter.h" #include "utils/time.h" +#include #include #include #include @@ -63,7 +64,7 @@ using utils::Logger; #define DEFAULT_STATS_FILE "manaserv.stats" #define DEFAULT_ATTRIBUTEDB_FILE "attributes.xml" -static bool running = true; /**< Determines if server keeps running */ +static std::atomic_bool running = true; /**< Determines if server keeps running */ utils::StringFilter *stringFilter; /**< Slang's Filter */ @@ -83,7 +84,7 @@ BandwidthMonitor *gBandwidth; /** Callback used when SIGQUIT signal is received. */ static void closeGracefully(int) { - running = false; + running.store(false, std::memory_order_relaxed); } /** @@ -455,7 +456,7 @@ int main(int argc, char *argv[]) // Start a thread to close the sockets and timers when running is set to false std::thread([&wsApp,processTimer,statTimer,banTimer] { - while (running) { + while (running.load(std::memory_order_relaxed)) { std::this_thread::sleep_for(std::chrono::milliseconds(100)); } -- cgit v1.2.3-70-g09d2