Apollo 10.0
自动驾驶开放平台
concurrent_queue.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 <queue>
19
21
22namespace apollo {
23namespace perception {
24namespace lib {
25
26template <class Data>
28 public:
30 virtual ~ConcurrentQueue() {}
31
32 virtual void Push(const Data &data) {
33 MutexLock lock(&mutex_);
34 queue_.push(data);
36 }
37
38 virtual void Pop(Data *data) {
39 MutexLock lock(&mutex_);
40
41 while (queue_.empty()) {
43 }
44 *data = queue_.front();
45 queue_.pop();
46 }
47
48 bool TryPop(Data *data) {
49 MutexLock lock(&mutex_);
50
51 if (queue_.empty()) {
52 return false;
53 }
54
55 *data = queue_.front();
56 queue_.pop();
57 return true;
58 }
59
60 bool Empty() {
61 MutexLock lock(&mutex_);
62 return queue_.empty();
63 }
64
65 int Size() {
66 MutexLock lock(&mutex_);
67 return static_cast<int>(queue_.size());
68 }
69
70 void Clear() {
71 MutexLock lock(&mutex_);
72 while (!queue_.empty()) {
73 queue_.pop();
74 }
75 }
76
79
80 protected:
81 std::queue<Data> queue_;
84
85 private:
86};
87
88template <typename Data>
89class FixedSizeConQueue : public ConcurrentQueue<Data> {
90 public:
91 explicit FixedSizeConQueue(size_t max_count)
92 : ConcurrentQueue<Data>(), max_count_(max_count) {}
93
94 virtual ~FixedSizeConQueue() {}
95
96 virtual void Push(const Data &data) {
97 MutexLock lock(&this->mutex_);
98 while (this->queue_.size() >= max_count_) {
99 condition_full_.Wait(&this->mutex_);
100 }
101 this->queue_.push(data);
103 }
104
105 virtual bool TryPush(const Data &data) {
106 MutexLock lock(&this->mutex_);
107 if (this->queue_.size() >= max_count_) {
108 return false;
109 }
110 this->queue_.push(data);
112 return true;
113 }
114
115 virtual void Pop(Data *data) {
116 MutexLock lock(&this->mutex_);
117
118 while (this->queue_.empty()) {
119 this->condition_variable_.Wait(&this->mutex_);
120 }
121 *data = this->queue_.front();
122 this->queue_.pop();
123 condition_full_.Signal();
124 }
125
126 virtual bool TryPop(Data *data) {
127 MutexLock lock(&this->mutex_);
128
129 if (this->queue_.empty()) {
130 return false;
131 }
132
133 *data = this->queue_.front();
134 this->queue_.pop();
135 condition_full_.Signal();
136 return true;
137 }
138
139 bool Full() const { return this->queue_.size() >= max_count_; }
140
143
144 private:
145 CondVar condition_full_;
146 const size_t max_count_;
147};
148
149} // namespace lib
150} // namespace perception
151} // namespace apollo
ConcurrentQueue & operator=(const ConcurrentQueue &)=delete
ConcurrentQueue(const ConcurrentQueue &)=delete
virtual void Push(const Data &data)
virtual bool TryPush(const Data &data)
virtual void Push(const Data &data)
FixedSizeConQueue & operator=(const FixedSizeConQueue &)=delete
FixedSizeConQueue(const FixedSizeConQueue &)=delete
class register implement
Definition arena_queue.h:37