-
Notifications
You must be signed in to change notification settings - Fork 41
/
example.cpp
619 lines (460 loc) · 15.3 KB
/
example.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
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
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
/*
* copyright: 2014-2017
* name : Francis Banyikwa
* email: [email protected]
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include "task.hpp"
#include "example.h"
#include <QString>
#include <QMetaObject>
#include <QCoreApplication>
#include <iostream>
static void _testing_task_await() ;
static void _testing_task_future_all() ;
static void _testing_multiple_tasks() ;
static void _testing_multiple_tasks_with_start() ;
static void _testing_queue_with_no_results() ;
static void _testing_queue_with_results() ;
static void _testing_checking_multiple_futures() ;
template< typename T >
static void _print( const T& e )
{
std::cout << e << std::endl ;
}
struct wait{
void task_finished( const char * s )
{
QMutexLocker m( &mutex ) ;
counter++ ;
std::cout << s << std::endl ;
if( counter == max ){
loop.exit() ;
}
}
int max = 3 ;
int counter = 0 ;
QMutex mutex ;
QEventLoop loop ;
};
void example::start()
{
QMetaObject::invokeMethod( this,"run",Qt::QueuedConnection ) ;
}
QString _longRunningTask()
{
return "abc" ;
}
static void _printThreadID()
{
std::cout << "Thread id: " << QThread::currentThreadId() << std::endl ;
}
static void _useResult( const QString& e )
{
Q_UNUSED( e )
}
/*
* A sample use where a task is run on separate thread and return a value and the returned
* value is used on another task run on the original thread
*/
static void _test_run_then()
{
std::cout<< "Testing Task::run().then()" << std::endl ;
/*
* print the thread id to know we are on what thread.
* We are on the original thread here.
*/
_printThreadID() ;
Task::run( [](){
/*
* print the thread id to know we are on what thread.
* We are on a separate thread here
*/
_printThreadID() ;
/*
* Do a time consuming process on a separate thread and return its result
*/
return _longRunningTask() ;
} ).then( []( QString r ){
/*
* use the returned value by the previous task
*/
_useResult( r ) ;
/*
* print the thread id to know we are on what thread.
* We are back on the original thread and we will get here as a continuation of the task
* that completed above
*/
_printThreadID() ;
/*
* moving on to the next test.
*/
_testing_task_await() ;
} ) ;
}
/*
* Task::await() function below does the following:
* 1. suspends the "_testing_task_await" method at a point where Task::await() method is called.
* 2. creates a new thread.
* 3. runs the _longRunningTask method in the new thread.
* 4. store the result of _longRunningTask function in r.
* 5. resumes "_testing_task_await" method.
*/
static void _testing_task_await()
{
_print( "Testing Task::await()" ) ;
QString e = Task::await( _longRunningTask ) ;
_print( e.toLatin1().constData() ) ;
/*
* moving on to the next test.
*/
_testing_task_future_all() ;
}
/*
* Task::run() function below does the following:
* 1. Collects a bunch of tasks.
* 2. Runs each task on its own thread.
* 3. Returns a future that holds all above tasks.
* 4. .await() can be called on the future to suspend the current thread at a point
* where this medhod is called to wait for all tasks to finish.
* 5. .then() can be called on the future to register an event to be called when all
* tasks finish running.
*/
static void _testing_task_future_all()
{
auto fn1 = [](){ _printThreadID() ; } ;
auto fn2 = [](){ _printThreadID() ; } ;
auto fn3 = [](){ _printThreadID() ; } ;
_print( "Testing Task::run().await() multiple tasks" ) ;
Task::future<void>& e = Task::run_tasks( fn1,fn2,fn3 ) ;
e.await() ;
_print( "Testing Task::run().then() multiple tasks" ) ;
Task::future<void>& f1 = Task::run( fn1 ) ;
Task::future<void>& f2 = Task::run( fn2 ) ;
Task::future<void>& f3 = Task::run( fn3 ) ;
Task::future<void>& s = Task::run_tasks( f1,f2,f3 ) ;
s.then( [](){
/*
* moving on to the next test.
*/
_testing_multiple_tasks() ;
} ) ;
}
/*
* Task::run() function below does the following:
* 1. Collects a bunch of tasks and their continuations.
* 2. Runs each task on its own thread.
* 3. On completion of each task,run its continuation on the current thread.
* 4. Returns a future that holds all above tasks.
* 5. .await() can be called on the future to suspend the current thread at a point
* where this medhod is called to wait for all tasks and their continuations to finish.
* 6. .then() can be called on the future to register an event to be called when all
* tasks and their continuations finish.
*/
static void _testing_multiple_tasks_non_movable()
{
_print( "Testing multiple tasks without continuation arguments" ) ;
auto fna1 = [ a = std::unique_ptr<int>() ](){ _printThreadID(); } ;
auto fna2 = [ a = std::unique_ptr<int>() ](){ _printThreadID(); } ;
auto fna3 = [ a = std::unique_ptr<int>() ](){ _printThreadID(); } ;
auto ra1 = [ a = std::unique_ptr<int>() ](){ _print( "r1" ) ; } ;
auto ra2 = [ a = std::unique_ptr<int>() ](){ _print( "r2" ) ; } ;
auto ra3 = [ a = std::unique_ptr<int>() ](){ _print( "r3" ) ; } ;
Task::future<void>& e = Task::run( Task::make_pair( std::move( fna1 ),std::move( ra1 ) ),
Task::make_pair( std::move( fna2 ),std::move( ra2 ) ),
Task::make_pair( std::move( fna3 ),std::move( ra3 ) ) ) ;
e.await() ;
_print( "Testing multiple tasks with continuation arguments" ) ;
auto fn1 = [ a = std::unique_ptr<int>() ](){ _printThreadID() ; return 0 ; } ;
auto fn2 = [ a = std::unique_ptr<int>() ](){ _printThreadID() ; return 0 ; } ;
auto fn3 = [ a = std::unique_ptr<int>() ](){ _printThreadID() ; return 0 ; } ;
auto r1 = [ a = std::unique_ptr<int>() ]( int ){ _print( "r1" ) ; } ;
auto r2 = [ a = std::unique_ptr<int>() ]( int ){ _print( "r2" ) ; } ;
auto r3 = [ a = std::unique_ptr<int>() ]( int ){ _print( "r3" ) ; } ;
Task::future<int>& s = Task::run( Task::make_pair( std::move( fn1 ),std::move( r1 ) ),
Task::make_pair( std::move( fn2 ),std::move( r2 ) ),
Task::make_pair( std::move( fn3 ),std::move( r3 ) ) ) ;
s.then( _testing_multiple_tasks_with_start ) ;
}
static void _testing_multiple_tasks()
{
_print( "Testing multiple tasks without continuation arguments" ) ;
auto fna1 = [](){ _printThreadID(); } ;
auto fna2 = [](){ _printThreadID(); } ;
auto fna3 = [](){ _printThreadID(); } ;
auto ra1 = [](){ _print( "r1" ) ; } ;
auto ra2 = [](){ _print( "r2" ) ; } ;
auto ra3 = [](){ _print( "r3" ) ; } ;
Task::future<void>& e = Task::run( Task::make_pair( fna1,ra1 ),
Task::make_pair( fna2,ra2 ),
Task::make_pair( fna3,ra3 ) ) ;
e.await() ;
_print( "Testing multiple tasks with continuation arguments" ) ;
auto fn1 = [](){ _printThreadID() ; return 0 ; } ;
auto fn2 = [](){ _printThreadID() ; return 0 ; } ;
auto fn3 = [](){ _printThreadID() ; return 0 ; } ;
auto r1 = []( int ){ _print( "r1" ) ; } ;
auto r2 = []( int ){ _print( "r2" ) ; } ;
auto r3 = []( int ){ _print( "r3" ) ; } ;
Task::future<int>& s = Task::run( Task::make_pair( fn1,r1 ),
Task::make_pair( fn2,r2 ),
Task::make_pair( fn3,r3 ) ) ;
s.then( _testing_multiple_tasks_non_movable ) ;
}
static void _testing_multiple_tasks_with_start()
{
std::cout<< "Testing multiple tasks with continuation arguments using start" << std::endl ;
wait w ;
auto fn1 = [](){ _printThreadID() ; return 0 ; } ;
auto fn2 = [](){ _printThreadID() ; return 0 ; } ;
auto fn3 = [](){ _printThreadID() ; return 0 ; } ;
auto r1 = [ & ]( int ){ w.task_finished( "r1" ) ; } ;
auto r2 = [ & ]( int ){ w.task_finished( "r2" ) ; } ;
auto r3 = [ & ]( int ){ w.task_finished( "r3" ) ; } ;
Task::future<int>& s = Task::run( Task::make_pair( fn1,r1 ),
Task::make_pair( fn2,r2 ),
Task::make_pair( fn3,r3 ) ) ;
s.start() ;
w.loop.exec() ;
QCoreApplication::quit() ;
}
static void _testing_queue_with_no_results()
{
std::cout<< "Testing queue with no result" << std::endl ;
auto fna1 = [](){ _printThreadID(); } ;
auto fna2 = [](){ _printThreadID(); } ;
auto fna3 = [](){ _printThreadID(); } ;
auto ra1 = [](){ _print( "r1" ) ; } ;
auto ra2 = [](){ _print( "r2" ) ; } ;
auto ra3 = [](){ _print( "r3" ) ; } ;
Task::future<void>& e = Task::run( Task::make_pair( fna1,ra1 ),
Task::make_pair( fna2,ra2 ),
Task::make_pair( fna3,ra3 ) ) ;
e.queue( _testing_queue_with_results ) ;
}
static void _testing_queue_with_results()
{
std::cout<< "Testing queue with result" << std::endl ;
auto fn1 = [](){ _printThreadID() ; return 0 ; } ;
auto fn2 = [](){ _printThreadID() ; return 0 ; } ;
auto fn3 = [](){ _printThreadID() ; return 0 ; } ;
auto r1 = [ = ]( int ){ _print( "r1" ) ; } ;
auto r2 = [ = ]( int ){ _print( "r2" ) ; } ;
auto r3 = [ = ]( int ){ _print( "r3" ) ; } ;
Task::future<int>& s = Task::run( Task::make_pair( fn1,r1 ),
Task::make_pair( fn2,r2 ),
Task::make_pair( fn3,r3 ) ) ;
s.queue( _test_run_then ) ;
}
static void _testing_checking_multiple_futures()
{
auto fn1 = [](){} ;
auto fn2 = [](){} ;
auto fn3 = [](){} ;
_print( "Testing finding out if a future manages multiple futures" ) ;
Task::future<void>& e = Task::run_tasks( fn1,fn2,fn3 ) ;
const auto& z = e.all_threads() ;
std::string s = e.manages_multiple_futures() ? "true" : "false" ;
_print( "A future managed multiple futures: " + s ) ;
_print( "Number of future managed: " + QString::number( z.size() ).toStdString() ) ;
}
static void _test_when_any1()
{
_print( "Testing when_any" ) ;
wait w ;
auto ll1 = [ & ](){
QThread::currentThread()->sleep( 5 ) ;
w.task_finished( "aaa" ) ;
} ;
auto ll2 = [ & ](){
QThread::currentThread()->sleep( 2 ) ;
w.task_finished( "bbb" ) ;
} ;
auto ll3 = [ & ](){
QThread::currentThread()->sleep( 3 ) ;
w.task_finished( "ccc" ) ;
} ;
Task::run_tasks( ll1,ll2,ll3 ).when_any( [](){
_print( "when_any called" ) ;
} ) ;
w.loop.exec() ;
_print( "Done testing when_any" ) ;
}
static void _test_when_any2()
{
_print( "Testing when_any with result" ) ;
wait w ;
auto fn1 = [ & ](){
QThread::currentThread()->sleep( 5 ) ;
w.task_finished( "aaa" ) ;
return 0 ;
} ;
auto fn2 = [ & ](){
QThread::currentThread()->sleep( 2 ) ;
w.task_finished( "bbb" ) ;
return 0 ;
} ;
auto fn3 = [ & ](){
QThread::currentThread()->sleep( 3 ) ;
w.task_finished( "ccc" ) ;
return 0 ;
} ;
auto ff = [](int){};
Task::future<int>& s = Task::run( Task::make_pair( fn1,ff ),
Task::make_pair( fn2,ff ),
Task::make_pair( fn3,ff ) ) ;
s.when_any( [](){
_print( "when_any called" ) ;
} ) ;
w.loop.exec() ;
_print( "Done testing when_any with result" ) ;
}
static void _test_move_only_callables()
{
Task::run( [ a = std::unique_ptr<int>() ]( int x ){ return x ; },4 ).get() ;
Task::run( [ a = std::unique_ptr<int>() ](int){},4 ).get() ;
Task::run( [ a = std::unique_ptr<int>() ](){ return 6 ; } ).get() ;
Task::run( [ a = std::unique_ptr<int>() ](){} ).get() ;
Task::await( [ a = std::unique_ptr<int>() ](){ return 6 ; } ) ;
auto& tt = Task::run( [ a = std::unique_ptr<int>() ](){ return 6 ; } ) ;
Task::await( tt ) ;
Task::await( Task::run( [ a = std::unique_ptr<int>() ](){} ) ) ;
Task::await( [ a = std::unique_ptr<int>() ]( int x ){ return x ; },4 ) ;
Task::await( [ a = std::unique_ptr<int>() ](int){},4 ) ;
Task::await( [ a = std::unique_ptr<int>() ](){ return 6 ; } ) ;
Task::await( [ a = std::unique_ptr<int>() ](){} ) ;
auto& zz = Task::run( [ a = std::unique_ptr<int>() ](){ return 6 ; } ) ;
Task::await( zz ) ;
Task::await( Task::run( [ a = std::unique_ptr<int>() ](){} ) ) ;
Task::exec( [ a = std::unique_ptr<int>() ]( int x ){ return x ; },4 ) ;
Task::exec( [ a = std::unique_ptr<int>() ](int){},4 ) ;
Task::exec( [ a = std::unique_ptr<int>() ](){ return 6 ; } ) ;
Task::exec( [ a = std::unique_ptr<int>() ](){} ) ;
Task::run_tasks( [ a = std::unique_ptr<int>() ](){ return 6 ; },
[ a = std::unique_ptr<int>() ](){ return 6 ; },
[ a = std::unique_ptr<int>() ](){ return 6 ; },
[ a = std::unique_ptr<int>() ](){ return 6 ; } ).get() ;
Task::run_tasks( [](){ return 6 ; },
[](){ return 6 ; },
[](){ return 6 ; },
[](){ return 6 ; } ).get() ;
Task::run_tasks( Task::run( [ a = std::unique_ptr<int>() ](){} ),
Task::run( [ a = std::unique_ptr<int>() ](){} ),
Task::run( [ a = std::unique_ptr<int>() ](){} ) ).get() ;
}
static void _test_copyable_callables()
{
auto aa = []( int x ){ return x ; } ;
auto bb = [](){ return 6 ; } ;
auto cc = [](){} ;
auto dd = [](int){} ;
Task::run( aa,4 ).get() ;
Task::run( dd,4 ).get() ;
Task::run( bb ).get() ;
Task::run( cc ).get() ;
Task::await( bb ) ;
auto& tt = Task::run( bb ) ;
Task::await( tt ) ;
Task::await( Task::run( cc ) ) ;
Task::await( aa,4 ) ;
Task::await( dd,4 ) ;
Task::await( bb ) ;
Task::await( cc ) ;
auto& zz = Task::run( bb ) ;
Task::await( zz ) ;
Task::await( Task::run( cc ) ) ;
Task::exec( aa,4 ) ;
Task::exec( dd,4 ) ;
Task::exec( bb ) ;
Task::exec( cc ) ;
Task::run_tasks( bb,bb,cc ).get() ;
Task::run_tasks( Task::run( cc ),Task::run( cc ) ).get() ;
}
struct foo
{
foo()
{
}
foo(int a)
{
_print(a);
}
std::unique_ptr<int> m ;
};
struct www
{
void operator()()
{
}
www( const www& )
{
_print( "const www&" ) ;
}
www( www&& )
{
_print( "www&&" ) ;
}
www()
{
_print( "www()" ) ;
}
www& operator=( const www& )
{
_print( "www& operator=( const www& )" ) ;
return *this ;
}
www& operator=( www&& )
{
_print( "www& operator=( www&& )" ) ;
return *this ;
}
std::unique_ptr<int> m ;
};
void example::run()
{
#if __cplusplus >= 201703L
Task::await([](foo,foo,foo){},foo(7),foo(7),foo(7));
#endif
_test_copyable_callables() ;
_test_move_only_callables() ;
_test_when_any1() ;
_test_when_any2() ;
auto run_main = []( QVariant x ){
std::cout << x.value<int>() << ": mm Thread id: " << QThread::currentThreadId() << std::endl ;
} ;
auto run_bg = []( const Task::progress& pp ){
for( int i = 0 ; i < 2 ;i++ ){
std::cout << i << ": bg Thread id: " << QThread::currentThreadId() << std::endl ;
pp.update( i ) ;
QThread::currentThread()->sleep(1);
}
} ;
std::cout << "main Thread: " << QThread::currentThreadId() << std::endl ;
Task::future<void>& abc = Task::run( this,run_bg,run_main ) ;
abc.await() ;
_testing_checking_multiple_futures() ;
_testing_queue_with_no_results() ;
}