Apollo  6.0
Open source self driving car software
thread_worker.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 <condition_variable>
19 #include <functional>
20 #include <memory>
21 #include <thread>
22 
23 namespace apollo {
24 namespace perception {
25 namespace lib {
26 
27 class ThreadWorker {
28  public:
29  ThreadWorker() = default;
31 
32  // bind a bool returned function, should be called before start
33  void Bind(const std::function<bool()> &func);
34 
35  // start the thread loopy running
36  void Start();
37 
38  // wake up the thread to do something
39  void WakeUp();
40 
41  // wait util the one time execution is done
42  void Join();
43 
44  // release the thread resources
45  void Release();
46 
47  private:
48  // the main function of thread
49  void Core();
50 
51  private:
52  std::unique_ptr<std::thread> thread_ptr_;
53  std::mutex mutex_;
54  std::condition_variable condition_;
55 
56  bool work_flag_ = false;
57  bool exit_flag_ = true;
58 
59  std::function<bool()> work_func_;
60 };
61 
62 } // namespace lib
63 } // namespace perception
64 } // namespace apollo
void(* func)(void *)
Definition: routine_context.h:41
void Bind(const std::function< bool()> &func)
PlanningContext is the runtime context in planning. It is persistent across multiple frames...
Definition: atomic_hash_map.h:25
~ThreadWorker()
Definition: thread_worker.h:30
Definition: thread_worker.h:27