Grok 20.3.2
ConcurrentQueue.h
Go to the documentation of this file.
1/*
2 * Copyright (C) 2016-2026 Grok Image Compression Inc.
3 *
4 * This source code is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU Affero General Public License, version 3,
6 * as published by the Free Software Foundation.
7 *
8 * This source code is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU Affero General Public License for more details.
12 *
13 * You should have received a copy of the GNU Affero General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 *
16 */
17
18#pragma once
19
20#include <mutex>
21#include <condition_variable>
22#include <queue>
23
24namespace grk
25{
26
36template<typename T>
38{
39public:
40 ConcurrentQueue() = default;
41
42 void push(T item)
43 {
44 {
45 std::lock_guard<std::mutex> lock(mutex_);
46 queue_.push(std::move(item));
47 }
48 cv_.notify_one();
49 }
50
51 bool pop(T& item)
52 {
53 std::unique_lock<std::mutex> lock(mutex_);
54 cv_.wait(lock, [this] { return !queue_.empty() || closed_; });
55 if(queue_.empty())
56 return false;
57 item = std::move(queue_.front());
58 queue_.pop();
59 return true;
60 }
61
62 void close()
63 {
64 {
65 std::lock_guard<std::mutex> lock(mutex_);
66 closed_ = true;
67 }
68 cv_.notify_all();
69 }
70
71 size_t size() const
72 {
73 std::lock_guard<std::mutex> lock(mutex_);
74 return queue_.size();
75 }
76
77private:
78 std::queue<T> queue_;
79 bool closed_ = false;
80 mutable std::mutex mutex_;
81 std::condition_variable cv_;
82};
83
84} // namespace grk
void push(T item)
Definition ConcurrentQueue.h:42
ConcurrentQueue()=default
bool pop(T &item)
Definition ConcurrentQueue.h:51
bool closed_
Definition ConcurrentQueue.h:79
void close()
Definition ConcurrentQueue.h:62
std::condition_variable cv_
Definition ConcurrentQueue.h:81
size_t size() const
Definition ConcurrentQueue.h:71
std::queue< T > queue_
Definition ConcurrentQueue.h:78
std::mutex mutex_
Definition ConcurrentQueue.h:80
ResWindow.
Definition CompressedChunkCache.h:36