-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
executable file
·115 lines (97 loc) · 2.23 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
109
110
111
112
113
114
115
#include <thread>
#include <iostream>
#include <vector>
#include <random>
#include <cstdio>
#include <functional>
#include <mutex>
#include <algorithm>
#include <execinfo.h>
#include <signal.h>
#include <stdlib.h>
#include <unistd.h>
static const size_t TG_NUMBER = 1000000;
static const size_t TG_RANGE = 1000000;
struct talkgroup_t
{
uint32_t ssi;
char name[20];
};
std::vector<talkgroup_t> tg_database(TG_NUMBER);
std::mutex mutex;
void handler(int sig)
{
void *array[10];
size_t size;
// get void*'s for all entries on the stack
size = backtrace(array, 10);
// print out all the frames to stderr
printf("XXXX\n");
fprintf(stderr, "Error: signal %d:\n", sig);
backtrace_symbols_fd(array, size, STDERR_FILENO);
exit(1);
}
void fill_tg_database()
{
std::default_random_engine generator;
std::uniform_int_distribution<int> distribution(1, TG_RANGE);
for (auto it = tg_database.begin(); it != tg_database.end(); ++it)
{
it->ssi = distribution(generator);
std::sprintf(it->name, "TG: %d", it->ssi);
}
}
void working_thread()
{
std::default_random_engine generator;
std::uniform_int_distribution<int> distribution(1,TG_NUMBER);
auto randomize_tg = std::bind( distribution, generator );
while(true)
{
//mutex.lock();
tg_database.at(randomize_tg()-1).ssi = 0xabcdef;
//mutex.unlock();
}
}
void checking_thread()
{
int counter = 0;
while (true)
{
counter++;
auto it=tg_database.begin();
for(; it!=tg_database.end(); ++it)
{
if (it->ssi != 0xabcdef)
{
// printf("break\n");
break;
}
}
if (it==tg_database.end())
{
printf("end: %d\n", counter);
return;
}
printf("\r%d / %d", it-tg_database.begin(), TG_NUMBER);
}
}
int main()
{
signal(SIGSEGV, handler);
fill_tg_database();
std::vector<std::thread> worker_threads;
for(int i = 0; i < 5; ++i)
{
worker_threads.push_back(std::thread(working_thread));
}
std::thread checker_thread(checking_thread);
checker_thread.join();
std::for_each(worker_threads.begin(), worker_threads.end(),
[]
(std::thread& thread)
{
thread.detach();
});
return 0;
}