Skip to content

Commit cb43980

Browse files
authored
Merge pull request yhirose#184 from jaspervandeven/master
Made svr_sock_ and is_running_ variables atomic
2 parents 74e9a0d + 2fdc41c commit cb43980

File tree

1 file changed

+4
-4
lines changed

1 file changed

+4
-4
lines changed

httplib.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ typedef int socket_t;
7070
#include <string>
7171
#include <sys/stat.h>
7272
#include <thread>
73+
#include <atomic>
7374

7475
#ifdef CPPHTTPLIB_OPENSSL_SUPPORT
7576
#include <openssl/err.h>
@@ -285,8 +286,8 @@ class Server {
285286

286287
virtual bool read_and_close_socket(socket_t sock);
287288

288-
bool is_running_;
289-
socket_t svr_sock_;
289+
std::atomic<bool> is_running_;
290+
std::atomic<socket_t> svr_sock_;
290291
std::string base_dir_;
291292
Handlers get_handlers_;
292293
Handlers post_handlers_;
@@ -1587,8 +1588,7 @@ inline bool Server::is_running() const { return is_running_; }
15871588
inline void Server::stop() {
15881589
if (is_running_) {
15891590
assert(svr_sock_ != INVALID_SOCKET);
1590-
auto sock = svr_sock_;
1591-
svr_sock_ = INVALID_SOCKET;
1591+
std::atomic<socket_t> sock (svr_sock_.exchange(INVALID_SOCKET));
15921592
detail::shutdown_socket(sock);
15931593
detail::close_socket(sock);
15941594
}

0 commit comments

Comments
 (0)