1 先上死锁代码 如下:
#include "stdafx.h"
#include <mutex>
#include <thread>
#include <windows.h>
// windows系统中 std::mutex内部是通过Event内核对象实现的,而不是CRITICAL_SECTION
std::mutex mtx0;
std::mutex mtx1;
CRITICAL_SECTION cs0;
CRITICAL_SECTION cs1;
int _tmain(int argc, _TCHAR* argv[])
{
InitializeCriticalSectionAndSpinCount(&cs0, 4000);
InitializeCriticalSectionAndSpinCount(&cs1, 4000);
std::thread thread0([]{
EnterCriticalSection(&cs0);
std::this_thread::sleep_for(std::chrono::seconds(3));
EnterCriticalSection(&cs1);
});
std::thread thread1([]{
EnterCriticalSection(&