-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtask_queue.h
More file actions
29 lines (23 loc) · 847 Bytes
/
Copy pathtask_queue.h
File metadata and controls
29 lines (23 loc) · 847 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#pragma once
#include <condition_variable>
#include <mutex>
#include <queue>
#include "mmap_reader.h"
// A bounded-free, thread-safe queue of Chunks for the producer-consumer
// pipeline: one producer pushes chunks, many workers pop them. Uses a mutex +
// condition_variable (the spec's preferred MVP; a lock-free queue is a stretch
// goal). `close()` lets workers drain and then exit cleanly.
class TaskQueue {
public:
void push(const Chunk& chunk);
// Block until a chunk is available or the queue is closed-and-empty. Returns
// true and fills `out` on success; returns false once drained after close().
bool pop(Chunk& out);
// Signal that no more chunks will be pushed; wakes all waiting workers.
void close();
private:
std::mutex mu_;
std::condition_variable cv_;
std::queue<Chunk> q_;
bool closed_ = false;
};