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]() {
56 IsPoolThreadFlag() = true;
57 Run();
58 });
59 }
60
61 /// Whether the calling thread is a ThreadPool worker (it must not destroy its own pool).
62 static bool IsPoolThread() {
63 return IsPoolThreadFlag();
64 }
65
66 /// @copydoc AsyncRun(size_t)
67 void AsyncRun() {
68 AsyncRun(std::thread::hardware_concurrency());
69 }
70
71 /// Run tasks in this thread.
72 ///
73 /// @warning This function blocks until the ThreadPool has been stopped.
74 void Run() {
75 _io_context.run();
76 }
77
78 /// Run tasks in this thread for an specific @a duration.
79 ///
80 /// @warning This function blocks until the ThreadPool has been stopped, or
81 /// until the specified time duration has elapsed.
82 void RunFor(time_duration duration) {
83 _io_context.run_for(duration.to_chrono());
84 }
85
86 /// Stop the ThreadPool and join all its threads.
87 void Stop() {
88 _work_to_do.reset();
89 _io_context.stop();
91 }
92
93 private:
94
95 static bool &IsPoolThreadFlag() {
96 static thread_local bool is_pool_thread = false;
97 return is_pool_thread;
98 }
99
100 boost::asio::io_context _io_context;
101
102 boost::asio::executor_work_guard<boost::asio::io_context::executor_type> _work_to_do;
103
105 };
106
107} // namespace carla
NonCopyable()=default
void CreateThreads(size_t count, F functor)
Definition ThreadGroup.h:32
ThreadGroup _workers
Definition ThreadPool.h:104
void Stop()
Stop the ThreadPool and join all its threads.
Definition ThreadPool.h:87
void AsyncRun()
Launch threads to run tasks asynchronously.
Definition ThreadPool.h:67
boost::asio::executor_work_guard< boost::asio::io_context::executor_type > _work_to_do
Definition ThreadPool.h:102
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:82
void Run()
Run tasks in this thread.
Definition ThreadPool.h:74
boost::asio::io_context _io_context
Definition ThreadPool.h:100
static bool & IsPoolThreadFlag()
Definition ThreadPool.h:95
auto & io_context()
Return the underlying io_context.
Definition ThreadPool.h:36
static bool IsPoolThread()
Whether the calling thread is a ThreadPool worker (it must not destroy its own pool).
Definition ThreadPool.h:62
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