-
Notifications
You must be signed in to change notification settings - Fork 0
/
2_threads.cpp
79 lines (78 loc) · 1018 Bytes
/
2_threads.cpp
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#pragma region multi-threading
uint32_t g_x = 0;
uint32_t g_last = 10;
void task1()
{
g_x = 1;
}
void task2()
{
g_x = 2;
}
void task3()
{
g_x = 3;
}
void task4()
{
g_x = 4;
}
void task5()
{
g_x = 5;
}
void task6()
{
g_x = 6;
}
void task7()
{
g_x = 7;
}
void task8()
{
g_x = 8;
}
void task9()
{
g_x = 9;
}
void task10()
{
g_x = 10;
}
int main()
{
//write multi thread, create race condition
for (int i = 0; i < 100000; i++)
{
//thread t1(task1, g_x = 1);
std::thread t1(task1);
std::thread t2(task2);
std::thread t3(task3);
std::thread t4(task4);
std::thread t5(task5);
std::thread t6(task6);
std::thread t7(task7);
std::thread t8(task8);
std::thread t9(task9);
std::thread t10(task10);
t1.join();
t2.join();
t3.join();
t4.join();
t5.join();
t6.join();
t7.join();
t8.join();
t9.join();
t10.join();
std::cout << g_x << ",";
if (g_x != 10)
std::cout << "error\n";
}
std::cout << std::endl;
system("Pause");
return 0;
}
#pragma endregion multi-threading