Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lernen Thread-Safe Queues | Synchronization and Communication
Quizzes & Challenges
Quizzes
Challenges
/
C++ Concurrency and Multithreading

bookThread-Safe Queues

Thread-safe data structures are vital in concurrent programming because multiple threads may try to access or modify shared data simultaneously. Without proper synchronization, this can cause corrupted data, unpredictable results, or even crashes.

A thread-safe queue allows threads to safely add and remove items without interfering with each other. To achieve this, the underlying std::queue must be protected using synchronization tools like std::mutex for exclusive access and std::condition_variable to coordinate waiting and notifications between threads.

main.cpp

main.cpp

copy
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
#include <iostream> #include <queue> #include <thread> #include <mutex> #include <condition_variable> #include <optional> template <typename T> class ThreadSafeQueue { public: void push(const T& value) { std::lock_guard<std::mutex> lock(mtx_); queue_.push(value); cv_.notify_one(); } std::optional<T> pop() { std::unique_lock<std::mutex> lock(mtx_); cv_.wait(lock, [this]{ return !queue_.empty(); }); T value = queue_.front(); queue_.pop(); return value; } bool empty() const { std::lock_guard<std::mutex> lock(mtx_); return queue_.empty(); } private: mutable std::mutex mtx_; std::queue<T> queue_; std::condition_variable cv_; }; void producer(ThreadSafeQueue<int>& q) { for (int i = 0; i < 5; ++i) { q.push(i); std::cout << "Produced: " << i << std::endl; } } void consumer(ThreadSafeQueue<int>& q) { for (int i = 0; i < 5; ++i) { auto value = q.pop(); if (value) std::cout << "Consumed: " << *value << std::endl; } } int main() { ThreadSafeQueue<int> queue; std::thread t1(producer, std::ref(queue)); std::thread t2(consumer, std::ref(queue)); t1.join(); t2.join(); }
question mark

Which of the following statements correctly describes a safe way to implement a thread-safe queue using std::mutex and std::condition_variable?

Select the correct answer

War alles klar?

Wie können wir es verbessern?

Danke für Ihr Feedback!

Abschnitt 2. Kapitel 3

Fragen Sie AI

expand

Fragen Sie AI

ChatGPT

Fragen Sie alles oder probieren Sie eine der vorgeschlagenen Fragen, um unser Gespräch zu beginnen

Suggested prompts:

Can you explain how to implement a thread-safe queue in C++?

What are some best practices for avoiding lost wakeups and data races?

Can you give examples of lock-free data structures?

bookThread-Safe Queues

Swipe um das Menü anzuzeigen

Thread-safe data structures are vital in concurrent programming because multiple threads may try to access or modify shared data simultaneously. Without proper synchronization, this can cause corrupted data, unpredictable results, or even crashes.

A thread-safe queue allows threads to safely add and remove items without interfering with each other. To achieve this, the underlying std::queue must be protected using synchronization tools like std::mutex for exclusive access and std::condition_variable to coordinate waiting and notifications between threads.

main.cpp

main.cpp

copy
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
#include <iostream> #include <queue> #include <thread> #include <mutex> #include <condition_variable> #include <optional> template <typename T> class ThreadSafeQueue { public: void push(const T& value) { std::lock_guard<std::mutex> lock(mtx_); queue_.push(value); cv_.notify_one(); } std::optional<T> pop() { std::unique_lock<std::mutex> lock(mtx_); cv_.wait(lock, [this]{ return !queue_.empty(); }); T value = queue_.front(); queue_.pop(); return value; } bool empty() const { std::lock_guard<std::mutex> lock(mtx_); return queue_.empty(); } private: mutable std::mutex mtx_; std::queue<T> queue_; std::condition_variable cv_; }; void producer(ThreadSafeQueue<int>& q) { for (int i = 0; i < 5; ++i) { q.push(i); std::cout << "Produced: " << i << std::endl; } } void consumer(ThreadSafeQueue<int>& q) { for (int i = 0; i < 5; ++i) { auto value = q.pop(); if (value) std::cout << "Consumed: " << *value << std::endl; } } int main() { ThreadSafeQueue<int> queue; std::thread t1(producer, std::ref(queue)); std::thread t2(consumer, std::ref(queue)); t1.join(); t2.join(); }
question mark

Which of the following statements correctly describes a safe way to implement a thread-safe queue using std::mutex and std::condition_variable?

Select the correct answer

War alles klar?

Wie können wir es verbessern?

Danke für Ihr Feedback!

Abschnitt 2. Kapitel 3
some-alt