CARLA
Loading...
Searching...
No Matches
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:

Public Member Functions

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

Private Attributes

boost::asio::io_context _io_context
 
boost::asio::executor_work_guard< boost::asio::io_context::executor_type > _work_to_do
 
ThreadGroup _workers
 

Additional Inherited Members

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

Detailed Description

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

Definition at line 25 of file ThreadPool.h.

Constructor & Destructor Documentation

◆ ThreadPool()

carla::ThreadPool::ThreadPool ( )
inline

Definition at line 28 of file ThreadPool.h.

◆ ~ThreadPool()

carla::ThreadPool::~ThreadPool ( )
inline

Stops the ThreadPool and joins all its threads.

Definition at line 31 of file ThreadPool.h.

References Stop().

Member Function Documentation

◆ AsyncRun() [1/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 59 of file ThreadPool.h.

References AsyncRun().

Referenced by AsyncRun().

◆ AsyncRun() [2/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 54 of file ThreadPool.h.

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

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

◆ io_context()

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

Return the underlying io_context.

Definition at line 36 of file ThreadPool.h.

References _io_context.

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

◆ Post()

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

Post a task to the pool.

Definition at line 44 of file ThreadPool.h.

References _io_context, and carla::MoveHandler().

◆ Run()

void carla::ThreadPool::Run ( )
inline

Run tasks in this thread.

Warning
This function blocks until the ThreadPool has been stopped.

Definition at line 66 of file ThreadPool.h.

References _io_context.

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

◆ 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 74 of file ThreadPool.h.

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

◆ Stop()

void carla::ThreadPool::Stop ( )
inline

Member Data Documentation

◆ _io_context

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

Definition at line 87 of file ThreadPool.h.

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

◆ _work_to_do

boost::asio::executor_work_guard<boost::asio::io_context::executor_type> carla::ThreadPool::_work_to_do
private

Definition at line 89 of file ThreadPool.h.

Referenced by Stop().

◆ _workers

ThreadGroup carla::ThreadPool::_workers
private

Definition at line 91 of file ThreadPool.h.

Referenced by AsyncRun(), and Stop().


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