Robot Raconteur Core C++ Library
Loading...
Searching...
No Matches
RobotRaconteur::ThreadPool Class Reference

Thread pool for Robot Raconteur nodes. More...

#include <ThreadPool.h>

Inheritance diagram for RobotRaconteur::ThreadPool:
RobotRaconteur::IOContextThreadPool

Public Member Functions

 ThreadPool (const boost::shared_ptr< RobotRaconteurNode > &node)
 Construct a new ThreadPool.
virtual size_t GetThreadPoolCount ()
 Get the number of threads in the thread pool.
virtual void SetThreadPoolCount (size_t count)
 Set the desired number of threads in the thread pool.
virtual void Post (boost::function< void()> function)
 Post a function to be executed by the thread pool in a worker thread and return immediately.
virtual bool TryPost (boost::function< void()> function)
 Try posting a function to be executed by the thread pool in a worker thread and return immediately.
virtual boost::asio::io_context & get_io_context ()
 Get the boost::asio::io_context object.

Detailed Description

Thread pool for Robot Raconteur nodes.

See threading for more information on threading.

Robot Raconteur uses a Boost.Asio IO Context with a thread pool to handle network and other events. ThreadPool implements a multi-threaded pool that by default starts 20 threads. These threads wait for events and are dispatched by IO Context.

ThreadPool is normally created by the node using ThreadPoolFactory. The factory is configured using RobotRaconteurNode::SetThreadPoolFactory(). The thread pool may optionally be configured using the RobotRaconteurNode::SetThreadPool() if the thread pool is created directly.

The boost::asio::io_context object can be accessed using the ThreadPool::get_io_context() function. This can be used to initialize Boost.Asio objects like boost::asio::ip::tcp::socket and boost::asio::deadline_timer.

The IOContextThreadPool subclass of ThreadPool can be used if an external thread pool is used by an application. This is particularly useful for environments where single threaded operation is required, such as a simulation environment.

Constructor & Destructor Documentation

◆ ThreadPool()

RobotRaconteur::ThreadPool::ThreadPool ( const boost::shared_ptr< RobotRaconteurNode > & node)

Construct a new ThreadPool.

Must use boost::make_shared<ThreadPool>()

Use of ThreadPoolFactory is recommended.

Set the thread pool using RobotRaconteurNode::SetThreadPool()

Parameters
nodeThe node that owns the thread pool

Member Function Documentation

◆ get_io_context()

virtual boost::asio::io_context & RobotRaconteur::ThreadPool::get_io_context ( )
virtual

Get the boost::asio::io_context object.

Use the returned boost::asio::io_context reference to initialize Boost.Asio objects

Returns
boost::asio::io_context&

Reimplemented in RobotRaconteur::IOContextThreadPool.

◆ GetThreadPoolCount()

virtual size_t RobotRaconteur::ThreadPool::GetThreadPoolCount ( )
virtual

Get the number of threads in the thread pool.

Returns
size_t The number of threads

Reimplemented in RobotRaconteur::IOContextThreadPool.

◆ Post()

virtual void RobotRaconteur::ThreadPool::Post ( boost::function< void()> function)
virtual

Post a function to be executed by the thread pool in a worker thread and return immediately.

Parameters
functionThe function to execute

Reimplemented in RobotRaconteur::IOContextThreadPool.

◆ SetThreadPoolCount()

virtual void RobotRaconteur::ThreadPool::SetThreadPoolCount ( size_t count)
virtual

Set the desired number of threads in the thread pool.

Setting less than two threads may have unpredictable results

Parameters
countThe desired number of threads

Reimplemented in RobotRaconteur::IOContextThreadPool.

◆ TryPost()

virtual bool RobotRaconteur::ThreadPool::TryPost ( boost::function< void()> function)
virtual

Try posting a function to be executed by the thread pool in a worker thread and return immediately.

Parameters
functionThe function to execute
Returns
true The function was posted to the thread pool
false An error ocurred and the function was not posted

Reimplemented in RobotRaconteur::IOContextThreadPool.


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