Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Вивчайте 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

Все було зрозуміло?

Як ми можемо покращити це?

Дякуємо за ваш відгук!

Секція 2. Розділ 3

Запитати АІ

expand

Запитати АІ

ChatGPT

Запитайте про що завгодно або спробуйте одне із запропонованих запитань, щоб почати наш чат

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

Свайпніть щоб показати меню

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

Все було зрозуміло?

Як ми можемо покращити це?

Дякуємо за ваш відгук!

Секція 2. Розділ 3
some-alt