CARLA
ThreadGroup.h
Go to the documentation of this file.
1 // Copyright (c) 2017 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/Debug.h"
10 #include "carla/NonCopyable.h"
11 
12 #include <thread>
13 #include <vector>
14 
15 namespace carla {
16 
17  class ThreadGroup : private NonCopyable {
18  public:
19 
20  ThreadGroup() = default;
21 
23  JoinAll();
24  }
25 
26  template <typename F>
27  void CreateThread(F &&functor) {
28  _threads.emplace_back(std::forward<F>(functor));
29  }
30 
31  template <typename F>
32  void CreateThreads(size_t count, F functor) {
33  _threads.reserve(_threads.size() + count);
34  for (size_t i = 0u; i < count; ++i) {
35  CreateThread(functor);
36  }
37  }
38 
39  void JoinAll() {
40  for (auto &thread : _threads) {
41  DEBUG_ASSERT_NE(thread.get_id(), std::this_thread::get_id());
42  if (thread.joinable()) {
43  thread.join();
44  }
45  }
46  _threads.clear();
47  }
48 
49  private:
50 
51  std::vector<std::thread> _threads;
52  };
53 
54 } // namespace carla
#define DEBUG_ASSERT_NE(lhs, rhs)
Definition: Debug.h:77
void CreateThread(F &&functor)
Definition: ThreadGroup.h:27
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
ThreadGroup()=default
std::vector< std::thread > _threads
Definition: ThreadGroup.h:51
Inherit (privately) to suppress copy/move construction and assignment.