Appearance
Linked List Based SPSC Blocking Queue
In this exercise, you'll implement a thread-safe queue using:
- A singly-linked list as the underlying container
std::mutexandstd::condition_variablefor synchronization- Blocking
pop()behavior if the queue is empty
This is a basic example of a blocking queue used in multithreaded producer-consumer pipelines. While flexible, the use of dynamic memory makes it less predictable for latency-sensitive workloads.
> 💡 In real systems like logging or event pipelines, such queues help offload work from the main thread without busy waiting.
Requirements
push(const T&)should add to the back.pop()should block if the queue is empty until a new element arrives.- The queue should allow exactly one producer and one consumer thread.
Bonus: Implement an optional try_pop() and shutdown() method if you're feeling adventurous.
Implement a blocking queue using a linked list with one producer and one consumer.
cpp
#include <mutex>
#include <condition_variable>
template <typename T>
class BlockingQueue {
private:
struct Node;
// Data members here
public:
BlockingQueue() {
// TODO: Initialize data structures
}
~BlockingQueue() {
// TODO: Cleanup
}
void push(const T& val) {
// TODO: Push to queue
}
T pop() {
// TODO: Pop from queue
}
};