Config::thread_pool_size is no longer checked if io_service is set (that is, when external io_service is used) before calling start
This commit is contained in:
parent
b4756d0de3
commit
d1798396ac
1 changed files with 23 additions and 17 deletions
|
|
@ -196,7 +196,8 @@ namespace SimpleWeb {
|
||||||
public:
|
public:
|
||||||
/// Port number to use. Defaults to 80 for HTTP and 443 for HTTPS.
|
/// Port number to use. Defaults to 80 for HTTP and 443 for HTTPS.
|
||||||
unsigned short port;
|
unsigned short port;
|
||||||
/// Number of threads that the server will use when start() is called. Defaults to 1 thread.
|
/// If io_service is not set, number of threads that the server will use when start() is called.
|
||||||
|
/// Defaults to 1 thread.
|
||||||
size_t thread_pool_size = 1;
|
size_t thread_pool_size = 1;
|
||||||
/// Timeout on request handling. Defaults to 5 seconds.
|
/// Timeout on request handling. Defaults to 5 seconds.
|
||||||
size_t timeout_request = 5;
|
size_t timeout_request = 5;
|
||||||
|
|
@ -234,8 +235,10 @@ namespace SimpleWeb {
|
||||||
std::function<void(std::shared_ptr<socket_type> socket, std::shared_ptr<typename ServerBase<socket_type>::Request>)> on_upgrade;
|
std::function<void(std::shared_ptr<socket_type> socket, std::shared_ptr<typename ServerBase<socket_type>::Request>)> on_upgrade;
|
||||||
|
|
||||||
virtual void start() {
|
virtual void start() {
|
||||||
if(!io_service)
|
if(!io_service) {
|
||||||
io_service = std::make_shared<asio::io_service>();
|
io_service = std::make_shared<asio::io_service>();
|
||||||
|
internal_io_service = true;
|
||||||
|
}
|
||||||
|
|
||||||
if(io_service->stopped())
|
if(io_service->stopped())
|
||||||
io_service->reset();
|
io_service->reset();
|
||||||
|
|
@ -255,27 +258,29 @@ namespace SimpleWeb {
|
||||||
|
|
||||||
accept();
|
accept();
|
||||||
|
|
||||||
//If thread_pool_size>1, start m_io_service.run() in (thread_pool_size-1) threads for thread-pooling
|
if(internal_io_service) {
|
||||||
threads.clear();
|
//If thread_pool_size>1, start m_io_service.run() in (thread_pool_size-1) threads for thread-pooling
|
||||||
for(size_t c = 1; c < config.thread_pool_size; c++) {
|
threads.clear();
|
||||||
threads.emplace_back([this]() {
|
for(size_t c = 1; c < config.thread_pool_size; c++) {
|
||||||
|
threads.emplace_back([this]() {
|
||||||
|
io_service->run();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
//Main thread
|
||||||
|
if(config.thread_pool_size > 0)
|
||||||
io_service->run();
|
io_service->run();
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
//Main thread
|
//Wait for the rest of the threads, if any, to finish as well
|
||||||
if(config.thread_pool_size > 0)
|
for(auto &t : threads) {
|
||||||
io_service->run();
|
t.join();
|
||||||
|
}
|
||||||
//Wait for the rest of the threads, if any, to finish as well
|
|
||||||
for(auto &t : threads) {
|
|
||||||
t.join();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void stop() {
|
void stop() {
|
||||||
acceptor->close();
|
acceptor->close();
|
||||||
if(config.thread_pool_size > 0)
|
if(internal_io_service)
|
||||||
io_service->stop();
|
io_service->stop();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -288,10 +293,11 @@ namespace SimpleWeb {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// If you have your own asio::io_service, store its pointer here before running start().
|
/// If you have your own asio::io_service, store its pointer here before running start().
|
||||||
/// You might also want to set config.thread_pool_size to 0.
|
|
||||||
std::shared_ptr<asio::io_service> io_service;
|
std::shared_ptr<asio::io_service> io_service;
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
|
bool internal_io_service = false;
|
||||||
|
|
||||||
std::unique_ptr<asio::ip::tcp::acceptor> acceptor;
|
std::unique_ptr<asio::ip::tcp::acceptor> acceptor;
|
||||||
std::vector<std::thread> threads;
|
std::vector<std::thread> threads;
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue