CARLA
ThreadPool.h
Go to the documentation of this file.
1 // Copyright (c) 2019 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/io_context.hpp>
15 #include <boost/asio/post.hpp>
16 
17 #include <future>
18 #include <thread>
19 #include <type_traits>
20 
21 namespace carla {
22 
23  /// A thread pool based on Boost.Asio's io context.
24  class ThreadPool : private NonCopyable {
25  public:
26 
28 
29  /// Stops the ThreadPool and joins all its threads.
31  Stop();
32  }
33 
34  /// Return the underlying io_context.
35  auto &io_context() {
36  return _io_context;
37  }
38 
39  /// Post a task to the pool.
40  template <typename FunctorT, typename ResultT = typename std::result_of<FunctorT()>::type>
41  std::future<ResultT> Post(FunctorT &&functor) {
42  auto task = std::packaged_task<ResultT()>(std::forward<FunctorT>(functor));
43  auto future = task.get_future();
44  boost::asio::post(_io_context, carla::MoveHandler(task));
45  return future;
46  }
47 
48  /// Launch threads to run tasks asynchronously. Launch specific number of
49  /// threads if @a worker_threads is provided, otherwise use all available
50  /// hardware concurrency.
51  void AsyncRun(size_t worker_threads) {
52  _workers.CreateThreads(worker_threads, [this]() { Run(); });
53  }
54 
55  /// @copydoc AsyncRun(size_t)
56  void AsyncRun() {
57  AsyncRun(std::thread::hardware_concurrency());
58  }
59 
60  /// Run tasks in this thread.
61  ///
62  /// @warning This function blocks until the ThreadPool has been stopped.
63  void Run() {
64  _io_context.run();
65  }
66 
67  /// Run tasks in this thread for an specific @a duration.
68  ///
69  /// @warning This function blocks until the ThreadPool has been stopped, or
70  /// until the specified time duration has elapsed.
71  void RunFor(time_duration duration) {
72  _io_context.run_for(duration.to_chrono());
73  }
74 
75  /// Stop the ThreadPool and join all its threads.
76  void Stop() {
77  _io_context.stop();
78  _workers.JoinAll();
79  }
80 
81  private:
82 
83  boost::asio::io_context _io_context;
84 
85  boost::asio::io_context::work _work_to_do;
86 
88  };
89 
90 } // namespace carla
void AsyncRun()
Launch threads to run tasks asynchronously.
Definition: ThreadPool.h:56
~ThreadPool()
Stops the ThreadPool and joins all its threads.
Definition: ThreadPool.h:30
std::future< ResultT > Post(FunctorT &&functor)
Post a task to the pool.
Definition: ThreadPool.h:41
This file contains definitions of common data structures used in traffic manager. ...
Definition: Carla.cpp:133
void CreateThreads(size_t count, F functor)
Definition: ThreadGroup.h:32
void Run()
Run tasks in this thread.
Definition: ThreadPool.h:63
boost::asio::io_context::work _work_to_do
Definition: ThreadPool.h:85
void RunFor(time_duration duration)
Run tasks in this thread for an specific duration.
Definition: ThreadPool.h:71
A thread pool based on Boost.Asio&#39;s io context.
Definition: ThreadPool.h:24
boost::asio::io_context _io_context
Definition: ThreadPool.h:83
void Stop()
Stop the ThreadPool and join all its threads.
Definition: ThreadPool.h:76
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
Positive time duration up to milliseconds resolution.
Definition: Time.h:19
void AsyncRun(size_t worker_threads)
Launch threads to run tasks asynchronously.
Definition: ThreadPool.h:51
constexpr auto to_chrono() const
Definition: Time.h:50
Inherit (privately) to suppress copy/move construction and assignment.
auto & io_context()
Return the underlying io_context.
Definition: ThreadPool.h:35
ThreadGroup _workers
Definition: ThreadPool.h:87