Apollo 11.0
自动驾驶开放平台
sync_queue.h
浏览该文件的文档.
1/*********************************************************************************************************************
2Copyright (c) 2020 RoboSense
3All rights reserved
4
5By downloading, copying, installing or using the software you agree to this
6license. If you do not agree to this license, do not download, install, copy or
7use the software.
8
9License Agreement
10For RoboSense LiDAR SDK Library
11(3-clause BSD License)
12
13Redistribution and use in source and binary forms, with or without modification,
14are permitted provided that the following conditions are met:
15
161. Redistributions of source code must retain the above copyright notice, this
17list of conditions and the following disclaimer.
18
192. Redistributions in binary form must reproduce the above copyright notice,
20this list of conditions and the following disclaimer in the documentation and/or
21other materials provided with the distribution.
22
233. Neither the names of the RoboSense, nor Suteng Innovation Technology, nor the
24names of other contributors may be used to endorse or promote products derived
25from this software without specific prior written permission.
26
27THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
28ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
29WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
30DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
31ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
32(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
33LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
34ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
35(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
36SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37*********************************************************************************************************************/
38/******************************************************************************
39 * Copyright 2024 The Apollo Authors. All Rights Reserved.
40 *
41 * Licensed under the Apache License, Version 2.0 (the "License");
42 * you may not use this file except in compliance with the License.
43 * You may obtain a copy of the License at
44 *
45 * http://www.apache.org/licenses/LICENSE-2.0
46 *
47 * Unless required by applicable law or agreed to in writing, software
48 * distributed under the License is distributed on an "AS IS" BASIS,
49 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
50 * See the License for the specific language governing permissions and
51 * limitations under the License.
52 *****************************************************************************/
53#pragma once
54
55#include <condition_variable>
56#include <mutex>
57#include <queue>
58#include <thread>
59#include <utility>
60
61namespace apollo {
62namespace lidar {
63namespace drivers {
64
65template <typename T>
66class SyncQueue {
67 public:
68 inline size_t push(const T& value) {
69 bool empty = false;
70 size_t size = 0;
71
72 {
73 std::lock_guard<std::mutex> lg(mtx_);
74 empty = queue_.empty();
75 queue_.push(value);
76 size = queue_.size();
77 }
78
79 if (empty)
80 cv_.notify_one();
81 return size;
82 }
83
84 inline T pop() {
85 T value;
86
87 std::lock_guard<std::mutex> lg(mtx_);
88 if (!queue_.empty()) {
89 value = queue_.front();
90 queue_.pop();
91 }
92
93 return value;
94 }
95
96 inline bool popWait(T& ret_ele, unsigned int usec = 1000000) {
97 {
98 std::lock_guard<std::mutex> lg(mtx_);
99 if (!queue_.empty()) {
100 ret_ele = queue_.front();
101 queue_.pop();
102 return true;
103 }
104 }
105
106 std::this_thread::sleep_for(std::chrono::microseconds(1000));
107 return false;
108 }
109
110 inline void clear() {
111 std::queue<T> empty;
112 std::lock_guard<std::mutex> lg(mtx_);
113 swap(empty, queue_);
114 }
115
116 private:
117 std::queue<T> queue_;
118 std::mutex mtx_;
119 std::condition_variable cv_;
120};
121
122} // namespace drivers
123} // namespace lidar
124} // namespace apollo
bool popWait(T &ret_ele, unsigned int usec=1000000)
Definition sync_queue.h:96
size_t push(const T &value)
Definition sync_queue.h:68
class register implement
Definition arena_queue.h:37