Skip to content

Commit

Permalink
Merge pull request ipkn#307 from CrowCpp/concurrency_fix
Browse files Browse the repository at this point in the history
Fix inconsistencies in `concurrency`
The-EDev authored Dec 24, 2021
2 parents f4785cd + 7aee13e commit d668fa1
Showing 2 changed files with 13 additions and 13 deletions.
6 changes: 3 additions & 3 deletions include/crow/app.h
Original file line number Diff line number Diff line change
@@ -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";
20 changes: 10 additions & 10 deletions include/crow/http_server.h
Original file line number Diff line number Diff line change
@@ -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<std::future<void>> v;
std::atomic<int> 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,16 +138,15 @@ 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(
[&](const boost::system::error_code& /*error*/, int /*signal_number*/) {
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_;

0 comments on commit d668fa1

Please sign in to comment.