cppadvanced
C++ Thread-Safe Queue Push
Modern C++ std::lock_guard with condition variable notification
By ApeType Staff·17 lines·355 characters·1,420 total attempts
#include <queue>
#include <mutex>
#include <condition_variable>
template <typename T>
class SafeQueue {
private:
std::queue<T> data_;
mutable std::mutex mtx_;
std::condition_variable cv_;
public:
void push(T val) {
std::lock_guard<std::mutex> lock(mtx_);
data_.push(std::move(val));
cv_.notify_one();
}
};
TRANSCRIBE
Ready when you are.
Test your muscle memory and transcription speed against this exact snippet.
Start Typing
Tip: Press Esc anytime to restart
Best runs on this snippet
No recorded runs yet. Be the first to type this snippet!