RESTinio
Public Member Functions | Private Attributes | List of all members
restinio::on_pool_runner_t< Http_Server > Class Template Reference

Helper class for running an existing HTTP-server on a thread pool without blocking the current thread. More...

#include <http_server_run.hpp>

Public Member Functions

 on_pool_runner_t (const on_pool_runner_t &)=delete
 
 on_pool_runner_t (on_pool_runner_t &&)=delete
 
 on_pool_runner_t (std::size_t pool_size, Http_Server &server)
 Initializing constructor. More...
 
void start ()
 Start the server. More...
 
bool started () const noexcept
 Is server started. More...
 
void stop ()
 Stop the server. More...
 
void wait ()
 Wait for full stop of the server. More...
 

Private Attributes

Http_Server & m_server
 HTTP-server to be run. More...
 
impl::ioctx_on_thread_pool_t< impl::external_io_context_for_thread_pool_tm_pool
 Thread pool for running the server. More...
 

Detailed Description

template<typename Http_Server>
class restinio::on_pool_runner_t< Http_Server >

Helper class for running an existing HTTP-server on a thread pool without blocking the current thread.

Usage of run() functions has some drawbacks. For example, the current thread on that run() is called, will be blocked until run() returns.

Sometimes it is not appropriate and leads to tricks like that:

// HTTP-server to be run on a thread pool.
// Separate worker thread for calling restinio::run().
std::thread run_thread{ [&server] {
16,
server) );
// Now this thread is blocked until HTTP-server will be finished.
} };
... // Some application specific code here.
// Now the server can be stopped.
run_thread.join();

Writing such code is a boring and error-prone task. The class on_pool_runner_t can be used instead:

// HTTP-server to be run on a thread pool.
// Launch HTTP-server on a thread pool.
16,
server
};
... // Some application specific code here.
// Now the server can be stopped.
runner.stop(); // (1)
runner.wait();

Moreover the code at point (1) in the example above it not necessary because on_pool_runner_t automatically stops the server in the destructor.

Since
v.0.5.1

Definition at line 699 of file http_server_run.hpp.

Constructor & Destructor Documentation

◆ on_pool_runner_t() [1/3]

template<typename Http_Server >
restinio::on_pool_runner_t< Http_Server >::on_pool_runner_t ( const on_pool_runner_t< Http_Server > &  )
delete

◆ on_pool_runner_t() [2/3]

template<typename Http_Server >
restinio::on_pool_runner_t< Http_Server >::on_pool_runner_t ( on_pool_runner_t< Http_Server > &&  )
delete

◆ on_pool_runner_t() [3/3]

template<typename Http_Server >
restinio::on_pool_runner_t< Http_Server >::on_pool_runner_t ( std::size_t  pool_size,
Http_Server &  server 
)
inline

Initializing constructor.

Parameters
pool_sizeSize of thread pool.
serverServer instance to be run. NOTE. This reference must be valid for all life-time of on_pool_runner instance.

Definition at line 713 of file http_server_run.hpp.

Member Function Documentation

◆ start()

template<typename Http_Server >
void restinio::on_pool_runner_t< Http_Server >::start ( )
inline

Start the server.

Definition at line 726 of file http_server_run.hpp.

◆ started()

template<typename Http_Server >
bool restinio::on_pool_runner_t< Http_Server >::started ( ) const
inlinenoexcept

Is server started.

Definition at line 740 of file http_server_run.hpp.

◆ stop()

template<typename Http_Server >
void restinio::on_pool_runner_t< Http_Server >::stop ( )
inline

Stop the server.

Definition at line 744 of file http_server_run.hpp.

◆ wait()

template<typename Http_Server >
void restinio::on_pool_runner_t< Http_Server >::wait ( )
inline

Wait for full stop of the server.

Definition at line 758 of file http_server_run.hpp.

Member Data Documentation

◆ m_pool

template<typename Http_Server >
impl::ioctx_on_thread_pool_t< impl::external_io_context_for_thread_pool_t > restinio::on_pool_runner_t< Http_Server >::m_pool
private

Thread pool for running the server.

Definition at line 706 of file http_server_run.hpp.

◆ m_server

template<typename Http_Server >
Http_Server& restinio::on_pool_runner_t< Http_Server >::m_server
private

HTTP-server to be run.

Definition at line 702 of file http_server_run.hpp.


The documentation for this class was generated from the following file: