/************************************************************************* > File Name : demo3_dead_lock.cpp > Author : TL Song > EMail : songtianlun@frytea.com > Created Time : Mon Feb 8 08:54:17 2021 ************************************************************************/ #include #include #include #include #include #include int counter = 0; std::mutex mtx; // 保护counter void increase_proxy(int time, int id) { for (int i = 0; i < time; i++) { mtx.lock(); // 线程1上锁成功后,抛出异常:未释放锁 if (id == 1) { throw std::runtime_error("throw excption...."); } // 当前线程休眠1毫秒 std::this_thread::sleep_for(std::chrono::milliseconds(1)); counter++; mtx.unlock(); } } void increase(int time, int id) { try { increase_proxy(time, id); } catch (const std::exception& e){ std::cout << "id:" << id << ", " << e.what() << std::endl; } } int main(int argc, char** argv) { std::thread t1(increase, 10000, 1); std::thread t2(increase, 10000, 2); t1.join(); t2.join(); std::cout << "counter:" << counter << std::endl; return 0; }