I would like to explain threading deadlocks to newbies. I have seen many examples for deadlocks in the past, some using code and some using illustrations (like the famous 4
A sneaky way to deadlock with just a single thread is to try to lock the same (non-recursive) mutex twice. This might not be the simple example you were looking for, but sure enough I encountered such cases already.
#include <mutex>
#include <iostream>
int main()
{
std::mutex m;
m.lock();
m.lock();
std::cout << "Expect never to get here because of a deadlock!";
}
The producers-consumers problem together with the dining philosophers' problem is probably as simple as it's going to get. It has some pseudocode that illustrates it, as well. If those are too complex for a newbie they'd better try harder to grasp them.
Here's a simple example in C++11.
#include <mutex> // mutex
#include <iostream> // cout
#include <cstdio> // getchar
#include <thread> // this_thread, yield
#include <future> // async
#include <chrono> // seconds
using namespace std;
mutex _m1;
mutex _m2;
// Deadlock will occur because func12 and func21 acquires the two locks in reverse order
void func12()
{
unique_lock<mutex> l1(_m1);
this_thread::yield(); // hint to reschedule
this_thread::sleep_for( chrono::seconds(1) );
unique_lock<mutex> l2(_m2 );
}
void func21()
{
unique_lock<mutex> l2(_m2);
this_thread::yield(); // hint to reschedule
this_thread::sleep_for( chrono::seconds(1) );
unique_lock<mutex> l1(_m1);
}
int main( int argc, char* argv[] )
{
async(func12);
func21();
cout << "All done!"; // this won't be executed because of deadlock
getchar();
}
Deadlock can occur in a situation when a Girl1
is wanting to flirt with Guy2
, who is caught by another Girl2
, and Girl2
is wanting to flirt with a Guy1
that is caught by Girl1
. Since, both girls are waiting for dumping each other, the condition is called deadlock.
class OuchTheGirls
{
public static void main(String[] args)
{
final String resource1 = "Guy1";
final String resource2 = "Guy2";
// Girl1 tries to lock resource1 then resource2
Thread Girl1 = new Thread(() ->
{
synchronized (resource1)
{
System.out.println("Thread 1: locked Guy1");
try { Thread.sleep(100);} catch (Exception e) {}
synchronized (resource2)
{
System.out.println("Thread 1: locked Guy2");
}
}
});
// Girl2 tries to lock Guy2 then Guy1
Thread Girl2 = new Thread(() ->
{
synchronized (resource2)
{
System.out.println("Thread 2: locked Guy2");
try { Thread.sleep(100);} catch (Exception e) {}
synchronized (resource1)
{
System.out.println("Thread 2: locked Guy1");
}
}
});
Girl1.start();
Girl2.start();
}
}