CARLA
Public Member Functions | Private Attributes | List of all members
carla::ThreadPool Class Reference

A thread pool based on Boost.Asio's io context. More...

#include <ThreadPool.h>

+ Inheritance diagram for carla::ThreadPool:
+ Collaboration diagram for carla::ThreadPool:

Public Member Functions

void AsyncRun (size_t worker_threads)
 Launch threads to run tasks asynchronously. More...
 
void AsyncRun ()
 Launch threads to run tasks asynchronously. More...
 
auto & io_context ()
 Return the underlying io_context. More...
 
template<typename FunctorT , typename ResultT = typename std::result_of<FunctorT()>::type>
std::future< ResultT > Post (FunctorT &&functor)
 Post a task to the pool. More...
 
void Run ()
 Run tasks in this thread. More...
 
void RunFor (time_duration duration)
 Run tasks in this thread for an specific duration. More...
 
void Stop ()
 Stop the ThreadPool and join all its threads. More...
 
 ThreadPool ()
 
 ~ThreadPool ()
 Stops the ThreadPool and joins all its threads. More...
 

Private Attributes

boost::asio::io_context _io_context
 
boost::asio::io_context::work _work_to_do
 
ThreadGroup _workers
 

Additional Inherited Members

- Private Member Functions inherited from carla::NonCopyable
 NonCopyable ()=default
 
 NonCopyable (const NonCopyable &)=delete
 
 NonCopyable (NonCopyable &&)=delete
 
NonCopyableoperator= (const NonCopyable &)=delete
 
NonCopyableoperator= (NonCopyable &&)=delete
 

Detailed Description

A thread pool based on Boost.Asio's io context.

Definition at line 24 of file ThreadPool.h.

Constructor & Destructor Documentation

◆ ThreadPool()

carla::ThreadPool::ThreadPool ( )
inline

Definition at line 27 of file ThreadPool.h.

◆ ~ThreadPool()

carla::ThreadPool::~ThreadPool ( )
inline

Stops the ThreadPool and joins all its threads.

Definition at line 30 of file ThreadPool.h.

References Stop().

+ Here is the call graph for this function:

Member Function Documentation

◆ AsyncRun() [1/2]

void carla::ThreadPool::AsyncRun ( size_t  worker_threads)
inline

Launch threads to run tasks asynchronously.

Launch specific number of threads if worker_threads is provided, otherwise use all available hardware concurrency.

Definition at line 51 of file ThreadPool.h.

References _workers, carla::ThreadGroup::CreateThreads(), and Run().

Referenced by carla::multigpu::Router::AsyncRun(), carla::streaming::Client::AsyncRun(), carla::multigpu::Secondary::AsyncRun(), carla::streaming::Server::AsyncRun(), and TEST().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ AsyncRun() [2/2]

void carla::ThreadPool::AsyncRun ( )
inline

Launch threads to run tasks asynchronously.

Launch specific number of threads if worker_threads is provided, otherwise use all available hardware concurrency.

Definition at line 56 of file ThreadPool.h.

◆ io_context()

auto& carla::ThreadPool::io_context ( )
inline

Return the underlying io_context.

Definition at line 35 of file ThreadPool.h.

References _io_context.

Referenced by carla::multigpu::Router::Router(), and carla::streaming::Client::Subscribe().

+ Here is the caller graph for this function:

◆ Post()

template<typename FunctorT , typename ResultT = typename std::result_of<FunctorT()>::type>
std::future<ResultT> carla::ThreadPool::Post ( FunctorT &&  functor)
inline

Post a task to the pool.

Definition at line 41 of file ThreadPool.h.

References _io_context, and carla::MoveHandler().

Referenced by TEST().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ Run()

void carla::ThreadPool::Run ( )
inline

Run tasks in this thread.

Warning
This function blocks until the ThreadPool has been stopped.

Definition at line 63 of file ThreadPool.h.

References _io_context.

Referenced by AsyncRun(), carla::streaming::Client::Run(), and carla::streaming::Server::Run().

+ Here is the caller graph for this function:

◆ RunFor()

void carla::ThreadPool::RunFor ( time_duration  duration)
inline

Run tasks in this thread for an specific duration.

Warning
This function blocks until the ThreadPool has been stopped, or until the specified time duration has elapsed.

Definition at line 71 of file ThreadPool.h.

References _io_context, and carla::time_duration::to_chrono().

+ Here is the call graph for this function:

◆ Stop()

void carla::ThreadPool::Stop ( void  )
inline

Stop the ThreadPool and join all its threads.

Definition at line 76 of file ThreadPool.h.

References _io_context, _workers, and carla::ThreadGroup::JoinAll().

Referenced by carla::multigpu::Router::Stop(), carla::streaming::Client::~Client(), carla::multigpu::Secondary::~Secondary(), carla::streaming::Server::~Server(), and ~ThreadPool().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

Member Data Documentation

◆ _io_context

boost::asio::io_context carla::ThreadPool::_io_context
private

Definition at line 83 of file ThreadPool.h.

Referenced by io_context(), Post(), Run(), RunFor(), and Stop().

◆ _work_to_do

boost::asio::io_context::work carla::ThreadPool::_work_to_do
private

Definition at line 85 of file ThreadPool.h.

◆ _workers

ThreadGroup carla::ThreadPool::_workers
private

Definition at line 87 of file ThreadPool.h.

Referenced by AsyncRun(), and Stop().


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