Apollo  6.0
Open source self driving car software
thread_safe_queue.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 
17 #ifndef CYBER_BASE_THREAD_SAFE_QUEUE_H_
18 #define CYBER_BASE_THREAD_SAFE_QUEUE_H_
19 
20 #include <condition_variable>
21 #include <mutex>
22 #include <queue>
23 #include <thread>
24 #include <utility>
25 
26 namespace apollo {
27 namespace cyber {
28 namespace base {
29 
30 template <typename T>
32  public:
34  ThreadSafeQueue& operator=(const ThreadSafeQueue& other) = delete;
35  ThreadSafeQueue(const ThreadSafeQueue& other) = delete;
36 
38 
39  void Enqueue(const T& element) {
40  std::lock_guard<std::mutex> lock(mutex_);
41  queue_.emplace(element);
42  cv_.notify_one();
43  }
44 
45  bool Dequeue(T* element) {
46  std::lock_guard<std::mutex> lock(mutex_);
47  if (queue_.empty()) {
48  return false;
49  }
50  *element = std::move(queue_.front());
51  queue_.pop();
52  return true;
53  }
54 
55  bool WaitDequeue(T* element) {
56  std::unique_lock<std::mutex> lock(mutex_);
57  cv_.wait(lock, [this]() { return break_all_wait_ || !queue_.empty(); });
58  if (break_all_wait_) {
59  return false;
60  }
61  *element = std::move(queue_.front());
62  queue_.pop();
63  return true;
64  }
65 
66  typename std::queue<T>::size_type Size() {
67  std::lock_guard<std::mutex> lock(mutex_);
68  return queue_.size();
69  }
70 
71  bool Empty() {
72  std::lock_guard<std::mutex> lock(mutex_);
73  return queue_.empty();
74  }
75 
76  void BreakAllWait() {
77  break_all_wait_ = true;
78  cv_.notify_all();
79  }
80 
81  private:
82  volatile bool break_all_wait_ = false;
83  std::mutex mutex_;
84  std::queue<T> queue_;
85  std::condition_variable cv_;
86 };
87 
88 } // namespace base
89 } // namespace cyber
90 } // namespace apollo
91 
92 #endif // CYBER_BASE_THREAD_SAFE_QUEUE_H_
ThreadSafeQueue()
Definition: thread_safe_queue.h:33
~ThreadSafeQueue()
Definition: thread_safe_queue.h:37
std::queue< T >::size_type Size()
Definition: thread_safe_queue.h:66
PlanningContext is the runtime context in planning. It is persistent across multiple frames...
Definition: atomic_hash_map.h:25
bool WaitDequeue(T *element)
Definition: thread_safe_queue.h:55
void Enqueue(const T &element)
Definition: thread_safe_queue.h:39
bool Empty()
Definition: thread_safe_queue.h:71
bool Dequeue(T *element)
Definition: thread_safe_queue.h:45
Definition: thread_safe_queue.h:31
void BreakAllWait()
Definition: thread_safe_queue.h:76
ThreadSafeQueue & operator=(const ThreadSafeQueue &other)=delete