From 3c711089e58a1ffc6ce74ef4e0012fd06dc3065e Mon Sep 17 00:00:00 2001 From: yhirose Date: Mon, 16 Apr 2018 22:12:45 -0400 Subject: [PATCH] Temporary solution for #52 --- httplib.h | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/httplib.h b/httplib.h index 65de818..aabb0f8 100644 --- a/httplib.h +++ b/httplib.h @@ -52,6 +52,7 @@ typedef int socket_t; #include #include #include +#include #include #include #include @@ -231,6 +232,10 @@ private: Handlers post_handlers_; Handler error_handler_; Logger logger_; + + // TODO: Use thread pool... + std::mutex running_threads_mutex_; + int running_threads_; }; class Client { @@ -1407,6 +1412,7 @@ inline std::string SocketStream::get_remote_addr() { inline Server::Server(HttpVersion http_version) : http_version_(http_version) , svr_sock_(-1) + , running_threads_(0) { #ifndef _WIN32 signal(SIGPIPE, SIG_IGN); @@ -1632,10 +1638,29 @@ inline bool Server::listen_internal() // TODO: Use thread pool... std::thread([=]() { + { + std::lock_guard guard(running_threads_mutex_); + running_threads_++; + } + read_and_close_socket(sock); + + { + std::lock_guard guard(running_threads_mutex_); + running_threads_--; + } }).detach(); } + // TODO: Use thread pool... + for (;;) { + std::this_thread::sleep_for(std::chrono::milliseconds(10)); + std::lock_guard guard(running_threads_mutex_); + if (!running_threads_) { + break; + } + } + return ret; }