Apollo 10.0
自动驾驶开放平台
thread.h
浏览该文件的文档.
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
20namespace apollo {
21namespace perception {
22namespace lib {
23
24class 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_;
61 std::string thread_name_;
62
63 private:
64};
65
66} // namespace lib
67} // namespace perception
68} // namespace apollo
void set_thread_name(const std::string &name)
Definition thread.h:44
Thread & operator=(const Thread &)=delete
static void * ThreadRunner(void *arg)
Definition thread.h:52
std::string get_thread_name() const
Definition thread.h:43
Thread(bool joinable=false, const std::string &name="Thread")
Definition thread.h:26
Thread(const Thread &)=delete
pthread_t tid() const
Definition thread.h:29
void set_joinable(bool joinable)
Definition thread.h:31
class register implement
Definition arena_queue.h:37