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

bookAvoiding Deadlocks

Deadlocks are a critical concern in concurrent programming, especially when multiple threads attempt to acquire multiple locks. A deadlock occurs when two or more threads are each waiting for the other to release a resource, causing all of them to become stuck indefinitely. This situation often arises from specific patterns, such as when two threads acquire locks in different orders or when locks are held while waiting for another lock. Understanding the root causes of deadlocks is essential for designing robust concurrent systems.

main.cpp

main.cpp

copy
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
#include <iostream> #include <thread> #include <mutex> #include <chrono> // Two shared resources, each protected by a mutex std::mutex mutexA; std::mutex mutexB; void cause_deadlock() { std::lock_guard<std::mutex> lock1(mutexA); // Simulate some work std::this_thread::sleep_for(std::chrono::milliseconds(100)); std::lock_guard<std::mutex> lock2(mutexB); std::cout << "Thread 1 acquired both locks\n"; } void cause_deadlock_opposite() { std::lock_guard<std::mutex> lock1(mutexB); // Simulate some work std::this_thread::sleep_for(std::chrono::milliseconds(100)); std::lock_guard<std::mutex> lock2(mutexA); std::cout << "Thread 2 acquired both locks\n"; } void avoid_deadlock() { // Lock both mutexes in a fixed order using std::lock std::lock(mutexA, mutexB); std::lock_guard<std::mutex> lock1(mutexA, std::adopt_lock); std::lock_guard<std::mutex> lock2(mutexB, std::adopt_lock); std::cout << "Thread 3 acquired both locks safely\n"; } int main() { std::thread t1(cause_deadlock); std::thread t2(cause_deadlock_opposite); t1.join(); t2.join(); // Now demonstrate deadlock avoidance std::thread t3(avoid_deadlock); std::thread t4(avoid_deadlock); t3.join(); t4.join(); }

Deadlock prevention involves applying techniques that ensure your program cannot enter a deadlock state. Common strategies include:

  • Lock ordering: always acquire multiple locks in a consistent, predetermined order;
  • Using std::try_lock: attempt to acquire locks without blocking, and back off if unable to acquire all;
  • Avoiding nested locks: minimize the use of holding one lock while acquiring another.
question mark

Which code pattern is most likely to avoid deadlocks when working with multiple mutexes?

Select the correct answer

War alles klar?

Wie können wir es verbessern?

Danke für Ihr Feedback!

Abschnitt 2. Kapitel 2

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 more about how lock ordering prevents deadlocks?

What are some real-world examples of deadlocks in programming?

Can you provide code examples demonstrating deadlock prevention techniques?

bookAvoiding Deadlocks

Swipe um das Menü anzuzeigen

Deadlocks are a critical concern in concurrent programming, especially when multiple threads attempt to acquire multiple locks. A deadlock occurs when two or more threads are each waiting for the other to release a resource, causing all of them to become stuck indefinitely. This situation often arises from specific patterns, such as when two threads acquire locks in different orders or when locks are held while waiting for another lock. Understanding the root causes of deadlocks is essential for designing robust concurrent systems.

main.cpp

main.cpp

copy
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
#include <iostream> #include <thread> #include <mutex> #include <chrono> // Two shared resources, each protected by a mutex std::mutex mutexA; std::mutex mutexB; void cause_deadlock() { std::lock_guard<std::mutex> lock1(mutexA); // Simulate some work std::this_thread::sleep_for(std::chrono::milliseconds(100)); std::lock_guard<std::mutex> lock2(mutexB); std::cout << "Thread 1 acquired both locks\n"; } void cause_deadlock_opposite() { std::lock_guard<std::mutex> lock1(mutexB); // Simulate some work std::this_thread::sleep_for(std::chrono::milliseconds(100)); std::lock_guard<std::mutex> lock2(mutexA); std::cout << "Thread 2 acquired both locks\n"; } void avoid_deadlock() { // Lock both mutexes in a fixed order using std::lock std::lock(mutexA, mutexB); std::lock_guard<std::mutex> lock1(mutexA, std::adopt_lock); std::lock_guard<std::mutex> lock2(mutexB, std::adopt_lock); std::cout << "Thread 3 acquired both locks safely\n"; } int main() { std::thread t1(cause_deadlock); std::thread t2(cause_deadlock_opposite); t1.join(); t2.join(); // Now demonstrate deadlock avoidance std::thread t3(avoid_deadlock); std::thread t4(avoid_deadlock); t3.join(); t4.join(); }

Deadlock prevention involves applying techniques that ensure your program cannot enter a deadlock state. Common strategies include:

  • Lock ordering: always acquire multiple locks in a consistent, predetermined order;
  • Using std::try_lock: attempt to acquire locks without blocking, and back off if unable to acquire all;
  • Avoiding nested locks: minimize the use of holding one lock while acquiring another.
question mark

Which code pattern is most likely to avoid deadlocks when working with multiple mutexes?

Select the correct answer

War alles klar?

Wie können wir es verbessern?

Danke für Ihr Feedback!

Abschnitt 2. Kapitel 2
some-alt