maliput
ThreadPool Class Reference

Detailed Description

Creates a pool of threads and a pool of tasks to be executed by the threads simultaneously.

Expected use of this class follows:

Note
The API is not guaranteed to be thread safe. This class is provided to speed up build and load processes among others which are necessary but not part of the API. Use it with caution.

#include <include/maliput/utility/thread_pool.h>

Public Member Functions

 MALIPUT_NO_COPY_NO_MOVE_NO_ASSIGN (ThreadPool)
 
 ThreadPool (std::size_t n)
 Creates a pool of n threads. More...
 
 ~ThreadPool ()
 
template<class F , class R = std::result_of_t<F&()>>
std::future< R > Queue (F &&f)
 Adds tasks to the queue. More...
 
void Start ()
 Starts the threads to work on the tasks. More...
 
void Finish ()
 Finishes the threads. More...
 
void cancel_pending ()
 Cancels all non-started tasks. More...
 
bool is_running ()
 

Constructor & Destructor Documentation

◆ ThreadPool()

ThreadPool ( std::size_t  n)
explicit

Creates a pool of n threads.

Parameters
nIs the number of threads to be managed.
Exceptions
WhenN is equal to zero.

◆ ~ThreadPool()

~ThreadPool ( )

Member Function Documentation

◆ cancel_pending()

void cancel_pending ( )

Cancels all non-started tasks.

◆ Finish()

void Finish ( )

Finishes the threads.

Waits for all tasks previously added to finish before destroying the threads.

◆ is_running()

bool is_running ( )
Returns
True when the threads are already started.

◆ MALIPUT_NO_COPY_NO_MOVE_NO_ASSIGN()

MALIPUT_NO_COPY_NO_MOVE_NO_ASSIGN ( ThreadPool  )

◆ Queue()

std::future<R> Queue ( F &&  f)

Adds tasks to the queue.

Template Parameters
FIs the functor type.
Parameters
fIs the task.
Template Parameters
RIs the functors return type. @return A std::future<R> that will hold the result off`.
Exceptions
maliput::common::assertion_errorWhen Finish() method was already called.

◆ Start()

void Start ( )

Starts the threads to work on the tasks.

The main thread will not perform any task and will continue the execution.

Exceptions
maliput::common::assertion_errorWhen this method is called consecutively without finishing the threads.

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