fix data race on _thread

This commit is contained in:
Alexandre Konieczny 2019-04-29 16:18:22 +02:00 committed by Benjamin Sergeant
parent 6918f863b1
commit e37e69311b
2 changed files with 6 additions and 1 deletions

View File

@ -38,6 +38,7 @@ namespace ix
WebSocket::WebSocket() : WebSocket::WebSocket() :
_onMessageCallback(OnMessageCallback()), _onMessageCallback(OnMessageCallback()),
_stop(false), _stop(false),
_blocking(true),
_automaticReconnection(true), _automaticReconnection(true),
_handshakeTimeoutSecs(kDefaultHandShakeTimeoutSecs), _handshakeTimeoutSecs(kDefaultHandShakeTimeoutSecs),
_enablePong(kDefaultEnablePong), _enablePong(kDefaultEnablePong),
@ -135,6 +136,7 @@ namespace ix
{ {
if (_thread.joinable()) return; // we've already been started if (_thread.joinable()) return; // we've already been started
_blocking = false;
_thread = std::thread(&WebSocket::run, this); _thread = std::thread(&WebSocket::run, this);
} }
@ -156,6 +158,7 @@ namespace ix
_stop = true; _stop = true;
_thread.join(); _thread.join();
_stop = false; _stop = false;
_blocking = true;
_automaticReconnection = automaticReconnection; _automaticReconnection = automaticReconnection;
} }
@ -317,7 +320,8 @@ namespace ix
// 4. In blocking mode, getting out of this function is triggered by // 4. In blocking mode, getting out of this function is triggered by
// an explicit disconnection from the callback, or by the remote end // an explicit disconnection from the callback, or by the remote end
// closing the connection, ie isConnected() == false. // closing the connection, ie isConnected() == false.
if (!_thread.joinable() && !isConnected() && !_automaticReconnection) return; // closing the connection, ie isConnectedOrClosing() == false.
if (_blocking && !isConnectedOrClosing() && !_automaticReconnection) return;
} }
} }

View File

@ -154,6 +154,7 @@ namespace ix
static OnTrafficTrackerCallback _onTrafficTrackerCallback; static OnTrafficTrackerCallback _onTrafficTrackerCallback;
std::atomic<bool> _stop; std::atomic<bool> _stop;
std::atomic<bool> _blocking;
std::atomic<bool> _automaticReconnection; std::atomic<bool> _automaticReconnection;
std::thread _thread; std::thread _thread;
std::mutex _writeMutex; std::mutex _writeMutex;