diff --git a/net/Socket.cpp b/net/Socket.cpp index 9a8b5f136..47c8358e5 100644 --- a/net/Socket.cpp +++ b/net/Socket.cpp @@ -116,7 +116,7 @@ namespace { SocketPoll::SocketPoll(const std::string& threadName) : _name(threadName), _stop(false), - _threadStarted(false), + _threadStarted(0), _threadFinished(false), _runOnClientThread(false), _owner(std::this_thread::get_id()) @@ -166,23 +166,25 @@ bool SocketPoll::startThread() { assert(!_runOnClientThread); - if (!_threadStarted) + // In a race, only the first gets in. + if (_threadStarted++ == 0) { - _threadStarted = true; _threadFinished = false; _stop = false; try { - LOG_TRC("starting thread for poll " << _name); + LOG_TRC("Creating thread for poll " << _name); _thread = std::thread(&SocketPoll::pollingThreadEntry, this); return true; } catch (const std::exception& exc) { - LOG_ERR("Failed to start poll thread: " << exc.what()); - _threadStarted = false; + LOG_ERR("Failed to start poll thread [" << _name << "]: " << exc.what()); + _threadStarted = 0; } } + else + LOG_ERR("SocketPoll [" << _name << "] thread is already started."); return false; } @@ -205,7 +207,7 @@ void SocketPoll::joinThread() else { _thread.join(); - _threadStarted = false; + _threadStarted = 0; } } } diff --git a/net/Socket.hpp b/net/Socket.hpp index 62b6d721f..31a85d96b 100644 --- a/net/Socket.hpp +++ b/net/Socket.hpp @@ -663,8 +663,9 @@ public: void wakeup() { if (!isAlive()) - LOG_WRN("Waking up dead poll thread [" << _name << "], started: " << - _threadStarted << ", finished: " << _threadFinished); + LOG_WRN("Waking up dead poll thread [" + << _name << "], started: " << (_threadStarted ? "true" : "false") + << ", finished: " << _threadFinished); wakeup(_wakeup[1]); } @@ -810,7 +811,7 @@ private: std::atomic _stop; /// The polling thread. std::thread _thread; - std::atomic _threadStarted; + std::atomic _threadStarted; std::atomic _threadFinished; std::atomic _runOnClientThread; std::thread::id _owner;