修改程序中断
This commit is contained in:
@@ -35,6 +35,8 @@ void extract(cv::Mat &src);
|
||||
|
||||
double getPointLength(const cv::Point2f &p);
|
||||
|
||||
#include <mutex>
|
||||
|
||||
// 循环队列
|
||||
template<class type, int length>
|
||||
class RoundQueue {
|
||||
@@ -43,6 +45,7 @@ private:
|
||||
int head;
|
||||
int tail;
|
||||
int count;
|
||||
mutable std::mutex mtx;
|
||||
public:
|
||||
RoundQueue<type, length>() : head(0), tail(0), count(0) {};
|
||||
|
||||
@@ -51,14 +54,17 @@ public:
|
||||
};
|
||||
|
||||
int size() const {
|
||||
std::lock_guard<std::mutex> lock(mtx);
|
||||
return count;
|
||||
};
|
||||
|
||||
bool empty() const {
|
||||
std::lock_guard<std::mutex> lock(mtx);
|
||||
return count == 0;
|
||||
};
|
||||
|
||||
void push(const type &obj) {
|
||||
std::lock_guard<std::mutex> lock(mtx);
|
||||
data[head] = obj;
|
||||
head = (head + 1) % length;
|
||||
if (count < length) {
|
||||
@@ -69,15 +75,17 @@ public:
|
||||
};
|
||||
|
||||
bool pop(type &obj) {
|
||||
if (empty()) return false;
|
||||
std::lock_guard<std::mutex> lock(mtx);
|
||||
if (count == 0) return false;
|
||||
obj = data[tail];
|
||||
tail = (tail + 1) % length;
|
||||
count--;
|
||||
return true;
|
||||
};
|
||||
|
||||
type &operator[](int idx) {
|
||||
if (count == 0) return data[0];
|
||||
type operator[](int idx) const {
|
||||
std::lock_guard<std::mutex> lock(mtx);
|
||||
if (count == 0) return data[0]; // will return garbage but safe from crash
|
||||
if (idx < 0) idx += count;
|
||||
if (idx < 0) idx = 0;
|
||||
if (idx >= count) idx = count - 1;
|
||||
|
||||
Reference in New Issue
Block a user