File: thread_pool.cpp

package info (click to toggle)
mldemos 0.5.1-3
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 32,224 kB
  • ctags: 46,525
  • sloc: cpp: 306,887; ansic: 167,718; ml: 126; sh: 109; makefile: 2
file content (395 lines) | stat: -rw-r--r-- 13,580 bytes parent folder | download | duplicates (6)
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
// Copyright (C) 2008  Davis E. King (davis@dlib.net)
// License: Boost Software License   See LICENSE.txt for the full license.


#include <sstream>
#include <string>
#include <cstdlib>
#include <ctime>
#include <dlib/misc_api.h>
#include <dlib/threads.h>
#include <dlib/any.h>

#include "tester.h"

namespace  
{
    using namespace test;
    using namespace dlib;
    using namespace std;

    logger dlog("test.thread_pool");


    struct some_struct : noncopyable
    {
        float val;
    };

    int global_var = 0;

    struct add_functor
    {
        add_functor() { var = 1;}
        add_functor(int v):var(v) {}

        template <typename T, typename U, typename V>
        void operator()(T a, U b, V& res)
        {
            dlib::sleep(20);
            res = a + b;
        }

        void set_global_var() { global_var = 9; }
        void set_global_var_const() const { global_var = 9; }

        void set_global_var_arg1(int val) { global_var = val; }
        void set_global_var_const_arg1(int val) const { global_var = val; }
        void set_global_var_arg2(int val, int val2) { global_var = val+val2; }
        void set_global_var_const_arg2(int val, int val2) const { global_var = val+val2; }

        void operator()()
        {
            global_var = 9;
        }

        // use an any just so that if this object goes out of scope
        // then var will get all messed up.
        any var;
        void operator()(int& a) { dlib::sleep(100); a = var.get<int>(); }
        void operator()(int& a, int& b) { dlib::sleep(100); a = var.get<int>(); b = 2; }
        void operator()(int& a, int& b, int& c) { dlib::sleep(100); a = var.get<int>(); b = 2; c = 3; }
        void operator()(int& a, int& b, int& c, int& d) { dlib::sleep(100); a = var.get<int>(); b = 2; c = 3; d = 4; }
    };


    void set_global_var() {  global_var = 9; }

    void gset_struct_to_zero (some_struct& a) { a.val = 0; }
    void gset_to_zero (int& a) { a = 0; }
    void gincrement (int& a) { ++a; }
    void gadd (int a, const int& b, int& res) { dlib::sleep(20); res = a + b; }
    void gadd1(int& a, int& res) { res += a; }
    void gadd2 (int c, int a, const int& b, int& res) { dlib::sleep(20); res = a + b + c; }

    class thread_pool_tester : public tester
    {
    public:
        thread_pool_tester (
        ) :
            tester ("test_thread_pool",
                    "Runs tests on the thread_pool component.")
        {}

        void perform_test (
        )
        {
            add_functor f;
            for (int num_threads= 0; num_threads < 4; ++num_threads)
            {
                future<int> a, b, c, res, d;
                thread_pool tp(num_threads);
                print_spinner();

                future<some_struct> obj;


                for (int i = 0; i < 4; ++i)
                {
                    a = 1;
                    b = 2;
                    c = 3;
                    res = 4;


                    DLIB_TEST(a==a);
                    DLIB_TEST(a!=b);
                    DLIB_TEST(a==1);

                    tp.add_task(gset_to_zero, a);
                    tp.add_task(gset_to_zero, b);
                    tp.add_task(*this, &thread_pool_tester::set_to_zero, c);
                    tp.add_task(gset_to_zero, res);
                    DLIB_TEST(a == 0);
                    DLIB_TEST(b == 0);
                    DLIB_TEST(c == 0);
                    DLIB_TEST(res == 0);


                    tp.add_task(gincrement, a);
                    tp.add_task(*this, &thread_pool_tester::increment, b);
                    tp.add_task(*this, &thread_pool_tester::increment, c);
                    tp.add_task(gincrement, res);

                    DLIB_TEST(a == 1);
                    DLIB_TEST(b == 1);
                    DLIB_TEST(c == 1);
                    DLIB_TEST(res == 1);

                    tp.add_task(&gincrement, a);
                    tp.add_task(*this, &thread_pool_tester::increment, b);
                    tp.add_task(*this, &thread_pool_tester::increment, c);
                    tp.add_task(&gincrement, res);
                    tp.add_task(gincrement, a);
                    tp.add_task(*this, &thread_pool_tester::increment, b);
                    tp.add_task(*this, &thread_pool_tester::increment, c);
                    tp.add_task(gincrement, res);

                    DLIB_TEST(a == 3);
                    DLIB_TEST(b == 3);
                    DLIB_TEST(c == 3);
                    DLIB_TEST(res == 3);

                    tp.add_task(*this, &thread_pool_tester::increment, c);
                    tp.add_task(gincrement, res);
                    DLIB_TEST(c == 4);
                    DLIB_TEST(res == 4);


                    tp.add_task(gadd, a, b, res);
                    DLIB_TEST(res == a+b);
                    DLIB_TEST(res == 6);
                    a = 3;
                    b = 4;
                    res = 99;
                    DLIB_TEST(res == 99);
                    tp.add_task(*this, &thread_pool_tester::add, a, b, res);
                    DLIB_TEST(res == a+b);
                    DLIB_TEST(res == 7);

                    a = 1;
                    b = 2;
                    c = 3;
                    res = 88;
                    DLIB_TEST(res == 88);
                    DLIB_TEST(a == 1);
                    DLIB_TEST(b == 2);
                    DLIB_TEST(c == 3);

                    tp.add_task(gadd2, a, b, c, res);
                    DLIB_TEST(res == 6);
                    DLIB_TEST(a == 1);
                    DLIB_TEST(b == 2);
                    DLIB_TEST(c == 3);

                    a = 1;
                    b = 2;
                    c = 3;
                    res = 88;
                    DLIB_TEST(res == 88);
                    DLIB_TEST(a == 1);
                    DLIB_TEST(b == 2);
                    DLIB_TEST(c == 3);
                    tp.add_task(*this, &thread_pool_tester::add2, a, b, c, res);
                    DLIB_TEST(res == 6);
                    DLIB_TEST(a == 1);
                    DLIB_TEST(b == 2);
                    DLIB_TEST(c == 3);

                    a = 1;
                    b = 2;
                    c = 3;
                    res = 88;
                    tp.add_task(gadd1, a, b);
                    DLIB_TEST(a == 1);
                    DLIB_TEST(b == 3);
                    a = 2;
                    tp.add_task(*this, &thread_pool_tester::add1, a, b);
                    DLIB_TEST(a == 2);
                    DLIB_TEST(b == 5);


                    val = 4;
                    uint64 id = tp.add_task(*this, &thread_pool_tester::zero_val);
                    tp.wait_for_task(id);
                    DLIB_TEST(val == 0);
                    id = tp.add_task(*this, &thread_pool_tester::accum2, 1,2);
                    tp.wait_for_all_tasks();
                    DLIB_TEST(val == 3);
                    id = tp.add_task(*this, &thread_pool_tester::accum1, 3);
                    tp.wait_for_task(id);
                    DLIB_TEST(val == 6);


                    obj.get().val = 8;
                    DLIB_TEST(obj.get().val == 8);
                    tp.add_task(gset_struct_to_zero, obj);
                    DLIB_TEST(obj.get().val == 0);
                    obj.get().val = 8;
                    DLIB_TEST(obj.get().val == 8);
                    tp.add_task(*this,&thread_pool_tester::set_struct_to_zero, obj);
                    DLIB_TEST(obj.get().val == 0);

                    a = 1;
                    b = 2;
                    res = 0;
                    tp.add_task(f, a, b, res);
                    DLIB_TEST(a == 1);
                    DLIB_TEST(b == 2);
                    DLIB_TEST(res == 3);


                    global_var = 0;
                    DLIB_TEST(global_var == 0);
                    id = tp.add_task(&set_global_var);
                    tp.wait_for_task(id);
                    DLIB_TEST(global_var == 9);

                    global_var = 0;
                    DLIB_TEST(global_var == 0);
                    id = tp.add_task(f);
                    tp.wait_for_task(id);
                    DLIB_TEST(global_var == 9);

                    global_var = 0;
                    DLIB_TEST(global_var == 0);
                    id = tp.add_task(f, &add_functor::set_global_var);
                    tp.wait_for_task(id);
                    DLIB_TEST(global_var == 9);

                    global_var = 0;
                    a = 4;
                    DLIB_TEST(global_var == 0);
                    id = tp.add_task(f, &add_functor::set_global_var_arg1, a);
                    tp.wait_for_task(id);
                    DLIB_TEST(global_var == 4);

                    global_var = 0;
                    a = 4;
                    DLIB_TEST(global_var == 0);
                    id = tp.add_task_by_value(f, &add_functor::set_global_var_arg1, a);
                    tp.wait_for_task(id);
                    DLIB_TEST(global_var == 4);



                    global_var = 0;
                    a = 4;
                    b = 3;
                    DLIB_TEST(global_var == 0);
                    id = tp.add_task(f, &add_functor::set_global_var_arg2, a, b);
                    tp.wait_for_task(id);
                    DLIB_TEST(global_var == 7);

                    global_var = 0;
                    a = 4;
                    b = 3;
                    DLIB_TEST(global_var == 0);
                    id = tp.add_task_by_value(f, &add_functor::set_global_var_arg2, a, b);
                    tp.wait_for_task(id);
                    DLIB_TEST(global_var == 7);

                    global_var = 0;
                    a = 4;
                    b = 3;
                    DLIB_TEST(global_var == 0);
                    id = tp.add_task(f, &add_functor::set_global_var_const_arg2, a, b);
                    tp.wait_for_task(id);
                    DLIB_TEST(global_var == 7);

                    global_var = 0;
                    a = 4;
                    b = 3;
                    DLIB_TEST(global_var == 0);
                    id = tp.add_task_by_value(f, &add_functor::set_global_var_const_arg2, a, b);
                    tp.wait_for_task(id);
                    DLIB_TEST(global_var == 7);






                    global_var = 0;
                    a = 4;
                    DLIB_TEST(global_var == 0);
                    id = tp.add_task(f, &add_functor::set_global_var_const_arg1, a);
                    tp.wait_for_task(id);
                    DLIB_TEST(global_var == 4);

                    global_var = 0;
                    a = 4;
                    DLIB_TEST(global_var == 0);
                    id = tp.add_task_by_value(f, &add_functor::set_global_var_const_arg1, a);
                    tp.wait_for_task(id);
                    DLIB_TEST(global_var == 4);

                    global_var = 0;
                    DLIB_TEST(global_var == 0);
                    id = tp.add_task_by_value(f, &add_functor::set_global_var);
                    tp.wait_for_task(id);
                    DLIB_TEST(global_var == 9);


                    global_var = 0;
                    DLIB_TEST(global_var == 0);
                    id = tp.add_task(f, &add_functor::set_global_var_const);
                    tp.wait_for_task(id);
                    DLIB_TEST(global_var == 9);


                    global_var = 0;
                    DLIB_TEST(global_var == 0);
                    id = tp.add_task_by_value(f, &add_functor::set_global_var_const);
                    tp.wait_for_task(id);
                    DLIB_TEST(global_var == 9);



                }

                // add this task just to to perterb the thread pool before it goes out of scope
                tp.add_task(f, a, b, res);

                for (int k = 0; k < 3; ++k)
                {
                    print_spinner();
                    global_var = 0;
                    tp.add_task_by_value(add_functor());
                    tp.wait_for_all_tasks();
                    DLIB_TEST(global_var == 9);

                    a = 0; b = 0; c = 0; d = 0;
                    tp.add_task_by_value(add_functor(), a);
                    DLIB_TEST(a == 1);
                    a = 0; b = 0; c = 0; d = 0;
                    tp.add_task_by_value(add_functor(8), a, b);
                    DLIB_TEST(a == 8);
                    DLIB_TEST(b == 2);
                    a = 0; b = 0; c = 0; d = 0;
                    tp.add_task_by_value(add_functor(), a, b, c);
                    DLIB_TEST(a == 1);
                    DLIB_TEST(b == 2);
                    DLIB_TEST(c == 3);
                    a = 0; b = 0; c = 0; d = 0;
                    tp.add_task_by_value(add_functor(5), a, b, c, d);
                    DLIB_TEST(a == 5);
                    DLIB_TEST(b == 2);
                    DLIB_TEST(c == 3);
                    DLIB_TEST(d == 4);
                }

            }
        }

        long val;
        void accum1(long a) { val += a; }
        void accum2(long a, long b) { val += a + b; }
        void zero_val() { dlib::sleep(20); val = 0; }


        void set_struct_to_zero (some_struct& a) { a.val = 0; }
        void set_to_zero (int& a) { dlib::sleep(20); a = 0; }
        void increment (int& a) const { dlib::sleep(20); ++a; }
        void add (int a, const int& b, int& res) { dlib::sleep(20); res = a + b; }
        void add1(int& a, int& res) const { res += a; }
        void add2 (int c, int a, const int& b, int& res) { res = a + b + c; }


    } a;


}