CARLA
Loading...
Searching...
No Matches
ThreadPool.h
Go to the documentation of this file.
1// Copyright (c) 2026 Computer Vision Center (CVC) at the Universitat Autonoma
2// de Barcelona (UAB).
3//
4// This work is licensed under the terms of the MIT license.
5// For a copy, see <https://opensource.org/licenses/MIT>.
6
7#pragma once
8
9#include "carla/MoveHandler.h"
10#include "carla/NonCopyable.h"
11#include "carla/ThreadGroup.h"
12#include "carla/Time.h"
13
14#include <boost/asio/executor_work_guard.hpp>
15#include <boost/asio/io_context.hpp>
16#include <boost/asio/post.hpp>
17
18#include <future>
19#include <thread>
20#include <type_traits>
21
22namespace carla {
23
24 /// A thread pool based on Boost.Asio's io context.
25 class ThreadPool : private NonCopyable {
26 public:
27
28 ThreadPool() : _work_to_do(boost::asio::make_work_guard(_io_context)) {}
29
30 /// Stops the ThreadPool and joins all its threads.
32 Stop();
33 }
34
35 /// Return the underlying io_context.
36 auto &io_context() {
37 return _io_context;
38 }
39
40 /// Post a task to the pool.
41 template <
42 typename FunctorT,
43 typename ResultT = typename std::invoke_result_t<FunctorT()>>
44 std::future<ResultT> Post(FunctorT &&functor) {
45 auto task = std::packaged_task<ResultT()>(std::forward<FunctorT>(functor));
46 auto future = task.get_future();
47 boost::asio::post(_io_context, carla::MoveHandler(task));
48 return future;
49 }
50
51 /// Launch threads to run tasks asynchronously. Launch specific number of
52 /// threads if @a worker_threads is provided, otherwise use all available
53 /// hardware concurrency.
54 void AsyncRun(size_t worker_threads) {
55 _workers.CreateThreads(worker_threads, [this]() { Run(); });
56 }
57
58 /// @copydoc AsyncRun(size_t)
59 void AsyncRun() {
60 AsyncRun(std::thread::hardware_concurrency());
61 }
62
63 /// Run tasks in this thread.
64 ///
65 /// @warning This function blocks until the ThreadPool has been stopped.
66 void Run() {
67 _io_context.run();
68 }
69
70 /// Run tasks in this thread for an specific @a duration.
71 ///
72 /// @warning This function blocks until the ThreadPool has been stopped, or
73 /// until the specified time duration has elapsed.
74 void RunFor(time_duration duration) {
75 _io_context.run_for(duration.to_chrono());
76 }
77
78 /// Stop the ThreadPool and join all its threads.
79 void Stop() {
80 _work_to_do.reset();
81 _io_context.stop();
83 }
84
85 private:
86
87 boost::asio::io_context _io_context;
88
89 boost::asio::executor_work_guard<boost::asio::io_context::executor_type> _work_to_do;
90
92 };
93
94} // namespace carla
Inherit (privately) to suppress copy/move construction and assignment.
void CreateThreads(size_t count, F functor)
Definition ThreadGroup.h:32
A thread pool based on Boost.Asio's io context.
Definition ThreadPool.h:25
ThreadGroup _workers
Definition ThreadPool.h:91
void Stop()
Stop the ThreadPool and join all its threads.
Definition ThreadPool.h:79
void AsyncRun()
Launch threads to run tasks asynchronously.
Definition ThreadPool.h:59
boost::asio::executor_work_guard< boost::asio::io_context::executor_type > _work_to_do
Definition ThreadPool.h:89
void AsyncRun(size_t worker_threads)
Launch threads to run tasks asynchronously.
Definition ThreadPool.h:54
~ThreadPool()
Stops the ThreadPool and joins all its threads.
Definition ThreadPool.h:31
void RunFor(time_duration duration)
Run tasks in this thread for an specific duration.
Definition ThreadPool.h:74
void Run()
Run tasks in this thread.
Definition ThreadPool.h:66
boost::asio::io_context _io_context
Definition ThreadPool.h:87
auto & io_context()
Return the underlying io_context.
Definition ThreadPool.h:36
std::future< ResultT > Post(FunctorT &&functor)
Post a task to the pool.
Definition ThreadPool.h:44
Positive time duration up to milliseconds resolution.
Definition Time.h:20
constexpr auto to_chrono() const
Definition Time.h:54
This file contains definitions of common data structures used in traffic manager.
auto MoveHandler(FunctorT &&func)
Hack to trick asio into accepting move-only handlers, if the handler were actually copied it would re...
Definition MoveHandler.h:33