Demo entry 6637249

Example deadlock with 3 mutexes

   

Submitted by anonymous on Aug 31, 2017 at 00:50
Language: C++. Code size: 562 Bytes.

void deadlock3() {
  std::mutex m1, m2, m3;
  folly::Baton<> b1, b2, b3;

  auto t1 = std::thread([&] {
    std::lock_guard<std::mutex> g1(m1);
    b1.post(); b2.wait();
    std::lock_guard<std::mutex> g2(m2);
  });

  auto t2 = std::thread([&] {
    std::lock_guard<std::mutex> g2(m2);
    b2.post(); b3.wait();
    std::lock_guard<std::mutex> g3(m3);
  });

  auto t3 = std::thread([&] {
    std::lock_guard<std::mutex> g3(m3);
    b3.post(); b1.wait();
    std::lock_guard<std::mutex> g1(m1);
  });

  t1.join(); t2.join(); t3.join();
}

This snippet took 0.00 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).