scenario_simulator_v2 C++ API
task_queue.hpp
Go to the documentation of this file.
1 // Copyright 2015 TIER IV, Inc. All rights reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #ifndef CONCEALER__TASK_QUEUE_HPP_
16 #define CONCEALER__TASK_QUEUE_HPP_
17 
18 #include <atomic>
19 #include <exception>
20 #include <functional>
21 #include <mutex>
22 #include <queue>
23 #include <thread>
24 
25 namespace concealer
26 {
27 class TaskQueue
28 {
29  using Thunk = std::function<void()>;
30 
31  std::queue<Thunk> thunks;
32 
33  std::mutex thunks_mutex;
34 
35  std::thread dispatcher;
36 
37  std::atomic<bool> is_stop_requested = false;
38 
39  std::atomic<bool> is_thrown = false;
40 
41  std::exception_ptr thrown;
42 
43 public:
44  explicit TaskQueue();
45 
46  void stopAndJoin();
47 
48  ~TaskQueue();
49 
50  template <typename F>
51  decltype(auto) delay(F && f)
52  {
53  rethrow();
54  std::unique_lock lk(thunks_mutex);
55  thunks.emplace(std::forward<F>(f));
56  }
57 
58  bool exhausted() const noexcept;
59 
60  void rethrow() const;
61 };
62 } // namespace concealer
63 
64 #endif // CONCEALER__TASK_QUEUE_HPP_
Definition: task_queue.hpp:28
bool exhausted() const noexcept
Definition: task_queue.cpp:56
void stopAndJoin()
Definition: task_queue.cpp:46
TaskQueue()
Definition: task_queue.cpp:22
void rethrow() const
Definition: task_queue.cpp:58
decltype(auto) delay(F &&f)
Definition: task_queue.hpp:51
~TaskQueue()
Definition: task_queue.cpp:54
Definition: autoware.hpp:30