-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
198 lines (122 loc) · 2.94 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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
// #define DEBUG
///if you enable this you will have to destroy all threads yourself
#define HEXO_THREADING_UNTRACKEDTHREADS
#include "Release/HexoThreading.h"
using namespace Hexo;
int main() {
/// testing error app with lambdas
std::string s = "Threading Engine: ";
Threading::SetErrorCallback([&](HXRC_STATE state){
std::cout << s << state.ErrorString << '\n';
if (state.Code == HXRC_FATAL)exit(0);
});
/// testing the error app
HX_THREADING_ERROR_PRINT("Error App is Working");
//spawn engine
ThreadingEngine hxt = ThreadingEngine();
/// testing immediate threads
{
int num = 1645;
HXImmediateThread t = hxt.SpawnImmediateThread(num,
[&num](void* data){
num *= 10;
}
);
hxt.JoinThread(t);
std::cout << "Immediate thread: " << num << '\n';
};
////
/// testing worker threads
{
HXWorkerThread t = hxt.SpawnWorkerThread();
for (int i=0; i<10; i++){
int num = 1645 + i;
hxt.SubmitTask(t, num,
[](void* data){
int num = *(reinterpret_cast<int*>(data)) * 10;
std::cout << "Worker thread: " << num << '\n';
}
);
}
//wait for worker thread to callback
while (true){
volatile size_t s = hxt.GetQueueSize(t);
if (!s)break;
}
hxt.DestroyThread(t);
};
/////
/// testing dedicated threads
{
HXDedicatedThread t = hxt.SpawnDedicatedThread(
[](void* data){
int num = *(reinterpret_cast<int*>(data)) * 10;
std::cout << "Dedicated thread: " << num << '\n';
}
);
for (int i=0; i<10; i++){
int num = 1645 + i;
hxt.SubmitTask(t, num);
}
//wait for dedicated thread to callback
while (true){
volatile size_t s = hxt.GetQueueSize(t);
if (!s)break;
}
hxt.DestroyThread(t);
};
/////
/// testing worker threadpools
{
HXWorkerThreadPool t = hxt.SpawnWorkerPool(4);
int* numarray = new int[10];
for (int i=0; i<10; i++){
numarray[i] = 1645 + i;
hxt.SubmitTask(t, i,
[&numarray](void* data){
int i = *(reinterpret_cast<int*>(data));
numarray[i] *= 10;
}
);
}
//wait for worker thread pool to callback
while (true){
volatile size_t s = hxt.GetQueueSize(t);
if (!s)break;
}
hxt.DestroyPool(t);
for (size_t i = 0; i < 10; i++) {
std::cout << "Worker pool: " << numarray[i] << '\n';
}
delete[] numarray;
};
/////
/// testing dedicated threadpools
{
HXDedicatedThreadPool t = hxt.SpawnDedicatedPool(4,
[](void* data){
**(reinterpret_cast<int**>(data)) *= 10;
}
);
int* numarray = new int[10];
for (int i=0; i<10; i++){
numarray[i] = 1645 + i;
int* ptr = numarray+i;
hxt.SubmitTask(t, ptr);
}
//wait for dedicated thread pool to callback
while (true){
volatile size_t s = hxt.GetQueueSize(t);
if (!s)break;
}
hxt.DestroyPool(t);
for (size_t i = 0; i < 10; i++) {
std::cout << "Dedicated pool: " << numarray[i] << '\n';
}
delete[] numarray;
};
/////
/// testing engine destruction
hxt.Release();
return 0;
}