0% found this document useful (0 votes)
28 views

7 Deadlock

Deadlock occurs in multithreaded programs when two or more threads are blocked waiting for each other to release resources, resulting in a standstill. Deadlocks are often caused by threads acquiring locks in inconsistent orders. To avoid deadlocks, locks should be acquired in a consistent order, released as soon as possible, use timeouts, and avoid nested locks. Tools can detect deadlocks in programs.

Uploaded by

Andrew Vakulyuk
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views

7 Deadlock

Deadlock occurs in multithreaded programs when two or more threads are blocked waiting for each other to release resources, resulting in a standstill. Deadlocks are often caused by threads acquiring locks in inconsistent orders. To avoid deadlocks, locks should be acquired in a consistent order, released as soon as possible, use timeouts, and avoid nested locks. Tools can detect deadlocks in programs.

Uploaded by

Andrew Vakulyuk
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 4

### Deadlock Mutual Lock

#multithreading
[[(thread)]] [[1.th.parameters]] [[2.th.&return]] [[3.th.lambda]] [[4.th.methods]]
[[5.mutex]] [[6.mutex.lock_guard]] [[7.Deadlock]] [[8.recursive_mutex]] [[9.
unique_lock]]

---
Deadlock is a situation that arises in a multi-threaded program when two or more
threads are blocked and waiting for each other to release resources, resulting in a
standstill or a "deadlock". When a deadlock occurs, the threads involved are unable
to proceed, and the program may stop responding, resulting in a system crash.

Deadlocks are often caused by synchronization issues in multi-threaded programs.


For example, if one thread locks a mutex and then tries to lock another mutex while
still holding the first one, and another thread tries to lock the second mutex
while still holding the first one, a deadlock can occur.

To avoid deadlocks, it is important to follow certain best practices while writing


multi-threaded programs:

1. Acquire locks in a consistent order: If two or more threads need to acquire


multiple locks, they should do so in the same order to prevent deadlocks.

2. Release locks as soon as possible: Holding onto locks for an extended period of
time increases the risk of deadlocks.

3. Use timeouts: Using timeouts on locks can prevent deadlocks by ensuring that a
thread doesn't wait indefinitely for a lock.

4. Avoid nested locks: Nested locks are a common cause of deadlocks, as they can
make it difficult to acquire locks in the correct order.

5. Use deadlock detection tools: There are many tools available for detecting
deadlocks in C++ programs, such as Valgrind and Helgrind.

By following these best practices and being aware of potential synchronization


issues, it is possible to write multi-threaded programs that are less prone to
deadlocks.

## My Example
---
Deadlock:
```cpp
#include <iostream>
#include <mutex>
#include <thread>
#include "SimpleTimer.h"

using namespace std;

mutex mtx1;
mutex mtx2;

void Print1() {

mtx2.lock();

this_thread::sleep_for(chrono::milliseconds(1)); // Long Code Emulator


mtx1.lock();

for (int i = 0; i < 5; ++i) {


for (int i = 0; i < 10; i++) {
cout << '*';
this_thread::sleep_for(chrono::milliseconds(10));
}
cout << endl;
}
cout << endl;

mtx1.unlock();
mtx2.unlock();

this_thread::sleep_for(chrono::milliseconds(2000)); // Long Code Emulator


}

void Print2() {

mtx1.lock();

this_thread::sleep_for(chrono::milliseconds(1)); // Long Code Emulator

mtx2.lock();

for (int i = 0; i < 5; ++i) {


for (int i = 0; i < 10; i++) {
cout << '#';
this_thread::sleep_for(chrono::milliseconds(10));
}
cout << endl;
}
cout << endl;

mtx1.unlock();
mtx2.unlock();

this_thread::sleep_for(chrono::milliseconds(2000)); // Long Code Emulator


}

int main() {

SimpleTimer timer;

thread t1(Print1);
thread t2(Print2);

t1.join();
t2.join();

return 0;
}
```

Solution
```cpp
#include <iostream>
#include <mutex>
#include <thread>
#include "SimpleTimer.h"

using namespace std;

mutex mtx1;
mutex mtx2;

void Print1() {

mtx1.lock();

//this_thread::sleep_for(chrono::milliseconds(1)); // Long Code Emulator

mtx2.lock();

for (int i = 0; i < 5; ++i) {


for (int i = 0; i < 10; i++) {
cout << '*';
this_thread::sleep_for(chrono::milliseconds(10));
}
cout << endl;
}
cout << endl;

mtx1.unlock();
mtx2.unlock();

this_thread::sleep_for(chrono::milliseconds(2000)); // Long Code Emulator


}

void Print2() {

mtx1.lock();

//this_thread::sleep_for(chrono::milliseconds(1)); // Long Code Emulator

mtx2.lock();

for (int i = 0; i < 5; ++i) {


for (int i = 0; i < 10; i++) {
cout << '#';
this_thread::sleep_for(chrono::milliseconds(10));
}
cout << endl;
}
cout << endl;

mtx1.unlock();
mtx2.unlock();

this_thread::sleep_for(chrono::milliseconds(2000)); // Long Code Emulator


}
int main() {

SimpleTimer timer;

thread t1(Print1);
thread t2(Print2);

t1.join();
t2.join();

return 0;
}
```

You might also like