-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
108 lines (93 loc) · 2.85 KB
/
main.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#include <iostream>
#include <thread>
#include <chrono>
#include <mutex>
#include <string.h>
#include "CShareMem.h"
using namespace std;
std::mutex thread_task1_mutex;
My_MEM* s_mem = new My_MEM;
void thread_task1(int n)
{
cout << "thread " << std::this_thread::get_id() << " begin!" << endl;
My_MEM new_mem;
for (int i = 0; i < n; i++)
{
std::lock_guard<std::mutex> lck(thread_task1_mutex);
s_mem->db3++;
memcpy(&new_mem, s_mem, sizeof(My_MEM));
}
cout << "thread " << std::this_thread::get_id() << " end! db3 is " << s_mem->db3 << endl;
}
const int s_sharemem_id = 0x090909;
void thread_task2(int n)
{
cout << "thread " << std::this_thread::get_id() << " begin!" << endl;
CShareMem *pShareMem = new CShareMem();
if (-1 == pShareMem->initShareMem(s_sharemem_id, false))
{
cout << "thread " << std::this_thread::get_id() << " initShareMem fail!" << endl;
return;
}
My_MEM new_mem;
for (int i = 0; i < n; i++)
{
if (-1 == pShareMem->updateGetData(&new_mem))
{
cout << "thread " << std::this_thread::get_id() << " readData fail." << endl;
break;
}
}
cout << "thread " << std::this_thread::get_id() << " end! db3 is " << new_mem.db3 << endl;
}
void RecordTime(const char *szOut)
{
std::chrono::time_point<std::chrono::system_clock, std::chrono::milliseconds> tp = std::chrono::time_point_cast<std::chrono::milliseconds>(std::chrono::system_clock::now());
std::time_t timestamp = tp.time_since_epoch().count();
if (szOut != NULL)
std::cout << "time " << szOut << " : " << timestamp << std::endl;
else
std::cout << "time : " << timestamp << std::endl;
}
// 测试分几种:程序内部内存的读写
// 使用共享内存的读写
int main(int argc, char *argv[])
{
RecordTime("开始测试");
const int max_thread_num = 2;
const int thread_while_time = 100000;
memset(s_mem, 0, sizeof(My_MEM));
if (true)
{
CShareMem *pShareMem = new CShareMem();
if (-1 == pShareMem->initShareMem(s_sharemem_id, true))
{
cout << "thread " << std::this_thread::get_id() << " initShareMem fail!" << endl;
return 1;
}
pShareMem->initData();
std::thread threads[max_thread_num];
for (int i = 0; i < max_thread_num; i++)
{
threads[i] = std::thread(thread_task2, thread_while_time);
}
for (auto &t : threads)
{
t.join();
}
}
else
{
std::thread threads[max_thread_num];
for (int i = 0; i < max_thread_num; i++)
{
threads[i] = std::thread(thread_task1, thread_while_time);
}
for (auto &t : threads)
{
t.join();
}
}
RecordTime("all thread finished.");
return 0;
}