Apollo  6.0
Open source self driving car software
thread.h
Go to the documentation of this file.
1 /******************************************************************************
2  * Copyright 2018 The Apollo Authors. All Rights Reserved.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *****************************************************************************/
16 #pragma once
17 
18 #include <string>
19 
20 namespace apollo {
21 namespace perception {
22 namespace lib {
23 
24 class Thread {
25  public:
26  explicit Thread(bool joinable = false, const std::string &name = "Thread")
27  : tid_(0), started_(false), joinable_(joinable), thread_name_(name) {}
28 
29  pthread_t tid() const { return tid_; }
30 
31  void set_joinable(bool joinable) {
32  if (!started_) {
33  joinable_ = joinable;
34  }
35  }
36 
37  void Start();
38 
39  void Join();
40 
41  bool IsAlive();
42 
43  std::string get_thread_name() const { return thread_name_; }
44  void set_thread_name(const std::string &name) { thread_name_ = name; }
45 
46  Thread(const Thread &) = delete;
47  Thread &operator=(const Thread &) = delete;
48 
49  protected:
50  virtual void Run() = 0;
51 
52  static void *ThreadRunner(void *arg) {
53  Thread *t = reinterpret_cast<Thread *>(arg);
54  t->Run();
55  return nullptr;
56  }
57 
58  pthread_t tid_;
59  bool started_;
60  bool joinable_;
61  std::string thread_name_;
62 
63  private:
64 };
65 
66 } // namespace lib
67 } // namespace perception
68 } // namespace apollo
bool started_
Definition: thread.h:59
pthread_t tid_
Definition: thread.h:58
PlanningContext is the runtime context in planning. It is persistent across multiple frames...
Definition: atomic_hash_map.h:25
void set_joinable(bool joinable)
Definition: thread.h:31
std::string thread_name_
Definition: thread.h:61
Thread(bool joinable=false, const std::string &name="Thread")
Definition: thread.h:26
Thread & operator=(const Thread &)=delete
Definition: thread.h:24
static void * ThreadRunner(void *arg)
Definition: thread.h:52
std::string get_thread_name() const
Definition: thread.h:43
bool joinable_
Definition: thread.h:60
void set_thread_name(const std::string &name)
Definition: thread.h:44
pthread_t tid() const
Definition: thread.h:29