diff --git a/include/crow/app.h b/include/crow/app.h index d9c87ab74..ec1b27e93 100644 --- a/include/crow/app.h +++ b/include/crow/app.h @@ -156,8 +156,8 @@ namespace crow /// Run the server on multiple threads using a specific number self_t& concurrency(std::uint16_t concurrency) { - if (concurrency < 1) - concurrency = 1; + if (concurrency < 2) // Crow can have a minimum of 2 threads running + concurrency = 2; concurrency_ = concurrency; return *this; } @@ -420,7 +420,7 @@ namespace crow private: std::uint8_t timeout_{5}; uint16_t port_ = 80; - uint16_t concurrency_ = 1; + uint16_t concurrency_ = 2; bool validated_ = false; std::string server_name_ = std::string("Crow/") + VERSION; std::string bindaddr_ = "0.0.0.0"; diff --git a/include/crow/http_server.h b/include/crow/http_server.h index e3b747e83..9cbedf0b5 100644 --- a/include/crow/http_server.h +++ b/include/crow/http_server.h @@ -31,12 +31,12 @@ namespace crow signals_(io_service_), tick_timer_(io_service_), handler_(handler), - concurrency_(concurrency == 0 ? 1 : concurrency), + concurrency_(concurrency), timeout_(timeout), server_name_(server_name), port_(port), bindaddr_(bindaddr), - task_queue_length_pool_(concurrency_), + task_queue_length_pool_(concurrency_ - 1), middlewares_(middlewares), adaptor_ctx_(adaptor_ctx) {} @@ -60,14 +60,15 @@ namespace crow void run() { - for (int i = 0; i < concurrency_; i++) + uint16_t worker_thread_count = concurrency_ - 1; + for (int i = 0; i < worker_thread_count; i++) io_service_pool_.emplace_back(new boost::asio::io_service()); - get_cached_date_str_pool_.resize(concurrency_); - task_timer_pool_.resize(concurrency_); + get_cached_date_str_pool_.resize(worker_thread_count); + task_timer_pool_.resize(worker_thread_count); std::vector> v; std::atomic init_count(0); - for (uint16_t i = 0; i < concurrency_; i++) + for (uint16_t i = 0; i < worker_thread_count; i++) v.push_back( std::async( std::launch::async, [this, i, &init_count] { @@ -137,8 +138,7 @@ namespace crow handler_->port(port_); - CROW_LOG_INFO << server_name_ << " server is running at " << (handler_->ssl_used() ? "https://" : "http://") << bindaddr_ << ":" << acceptor_.local_endpoint().port() - << " using " << concurrency_ << " threads"; + CROW_LOG_INFO << server_name_ << " server is running at " << (handler_->ssl_used() ? "https://" : "http://") << bindaddr_ << ":" << acceptor_.local_endpoint().port() << " using " << concurrency_ << " threads"; CROW_LOG_INFO << "Call `app.loglevel(crow::LogLevel::Warning)` to hide Info level logs."; signals_.async_wait( @@ -146,7 +146,7 @@ namespace crow stop(); }); - while (concurrency_ != init_count) + while (worker_thread_count != init_count) std::this_thread::yield(); do_accept(); @@ -232,7 +232,7 @@ namespace crow boost::asio::deadline_timer tick_timer_; Handler* handler_; - uint16_t concurrency_{1}; + uint16_t concurrency_{2}; std::uint8_t timeout_; std::string server_name_; uint16_t port_;