66#include < sys/socket.h>
77#include < sys/types.h>
88#include < unistd.h>
9+ #include < atomic>
910#include < cmath>
1011#include < cstring>
1112#include < deque>
@@ -73,7 +74,7 @@ class UDPSender final {
7374 bool m_isInitialized{false };
7475
7576 // ! Shall we exit?
76- bool m_mustExit{false };
77+ std::atomic< bool > m_mustExit{false };
7778
7879 // !@}
7980
@@ -118,7 +119,9 @@ class UDPSender final {
118119 std::optional<std::string> m_errorMessage;
119120};
120121
121- inline UDPSender::UDPSender (const std::string& host, const uint16_t port, const std::optional<uint64_t > batchsize) noexcept
122+ inline UDPSender::UDPSender (const std::string& host,
123+ const uint16_t port,
124+ const std::optional<uint64_t > batchsize) noexcept
122125 : m_host(host), m_port(port) {
123126 // If batching is on, use a dedicated thread to send every now and then
124127 if (batchsize) {
@@ -131,7 +134,7 @@ inline UDPSender::UDPSender(const std::string& host, const uint16_t port, const
131134
132135 // Define the batching thread
133136 m_batchingThread = std::thread ([this , batchingWait] {
134- while (!m_mustExit) {
137+ while (!m_mustExit. load (std::memory_order_acq_rel) ) {
135138 std::deque<std::string> stagedMessageQueue;
136139
137140 std::unique_lock<std::mutex> batchingLock (m_batchingMutex);
@@ -153,7 +156,7 @@ inline UDPSender::UDPSender(const std::string& host, const uint16_t port, const
153156
154157inline UDPSender::~UDPSender () {
155158 if (m_batching) {
156- m_mustExit = true ;
159+ m_mustExit. store ( true , std::memory_order_acq_rel) ;
157160 m_batchingThread.join ();
158161 }
159162
@@ -221,7 +224,7 @@ inline bool UDPSender::initialize() noexcept {
221224 hints.ai_socktype = SOCK_DGRAM;
222225
223226 // Get the address info using the hints
224- struct addrinfo * results = nullptr ;
227+ struct addrinfo * results = nullptr ;
225228 const int ret{getaddrinfo (m_host.c_str (), nullptr , &hints, &results)};
226229 if (ret != 0 ) {
227230 // An error code has been returned by getaddrinfo
@@ -232,7 +235,7 @@ inline bool UDPSender::initialize() noexcept {
232235 }
233236
234237 // Copy the results in m_server
235- struct sockaddr_in * host_addr = (struct sockaddr_in *)results->ai_addr ;
238+ struct sockaddr_in * host_addr = (struct sockaddr_in *)results->ai_addr ;
236239 std::memcpy (&m_server.sin_addr , &host_addr->sin_addr , sizeof (struct in_addr ));
237240
238241 // Free the memory allocated
@@ -251,7 +254,7 @@ inline void UDPSender::sendToDaemon(const std::string& message) noexcept {
251254
252255 // Try sending the message
253256 const long int ret{
254- sendto (m_socket, message.data (), message.size (), 0 , (struct sockaddr *)&m_server, sizeof (m_server))};
257+ sendto (m_socket, message.data (), message.size (), 0 , (struct sockaddr *)&m_server, sizeof (m_server))};
255258 if (ret == -1 ) {
256259 using namespace std ::string_literals;
257260 m_errorMessage =
0 commit comments