-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTestDeadLock.h
More file actions
49 lines (40 loc) · 988 Bytes
/
TestDeadLock.h
File metadata and controls
49 lines (40 loc) · 988 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#pragma once
#include <process.h>
#include <Windows.h>
/*
*/
CRITICAL_SECTION m_clsLock1;
CRITICAL_SECTION m_clsLock2;
int num = 0;
unsigned int _stdcall data_process1(void* lParam)
{
EnterCriticalSection(&m_clsLock1);
Sleep(5);
EnterCriticalSection(&m_clsLock2);
num++;
std::cout << num << std::endl;
LeaveCriticalSection(&m_clsLock2);
LeaveCriticalSection(&m_clsLock1);
return 0;
}
unsigned int _stdcall data_process2(void* lParam)
{
EnterCriticalSection(&m_clsLock2);
Sleep(5);
EnterCriticalSection(&m_clsLock1);
num++;
std::cout << num << std::endl;
LeaveCriticalSection(&m_clsLock1);
LeaveCriticalSection(&m_clsLock2);
return 0;
}
void TestDeadLock()
{
InitializeCriticalSection(&m_clsLock1);
InitializeCriticalSection(&m_clsLock2);
_beginthreadex(NULL, NULL, &data_process1, NULL, NULL, NULL);
_beginthreadex(NULL, NULL, &data_process2, NULL, NULL, NULL);
DeleteCriticalSection(&m_clsLock1);
DeleteCriticalSection(&m_clsLock2);
while (true);
}