summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorThorbjørn Lindeijer <bjorn@lindeijer.nl>2023-05-02 16:58:56 +0200
committerThorbjørn Lindeijer <bjorn@lindeijer.nl>2023-05-05 16:03:23 +0000
commit409b31423533ac3e648f882e4d2380dcf4fcc3ec (patch)
tree87b3e060963eb69354fd92fe6f64bcaed2144585
parent5612067c4f778b8d69e3173af3be7b59630f5674 (diff)
downloadmanaserv-409b31423533ac3e648f882e4d2380dcf4fcc3ec.tar.gz
manaserv-409b31423533ac3e648f882e4d2380dcf4fcc3ec.tar.bz2
manaserv-409b31423533ac3e648f882e4d2380dcf4fcc3ec.tar.xz
manaserv-409b31423533ac3e648f882e4d2380dcf4fcc3ec.zip
Avoid potential undefined behavior related to data race
Using atomic operations to avoid a compiler potentially optimizing away the thread responsible for clean shutdown.
-rw-r--r--src/account-server/main-account.cpp7
1 files changed, 4 insertions, 3 deletions
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 <atomic>
#include <cstdlib>
#include <getopt.h>
#include <csignal>
@@ -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));
}