FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
cpp-taskflow/unittest/threadpool_cxx14.cpp at master · navono/cpp-taskflow · GitHub
navono
/
cpp-taskflow
Public
forked from
taskflow/taskflow
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
cpp-taskflow
/
unittest
/
threadpool_cxx14.cpp
Copy path
More file actions
More file actions
Latest commit
History
History
History
99 lines (75 loc) · 2.53 KB
Breadcrumbs
cpp-taskflow
/
unittest
/
threadpool_cxx14.cpp
Copy path
File metadata and controls
99 lines (75 loc) · 2.53 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
//
2018/09/02 - contributed by Glen Fraser
//
- added wait_for_all test
//
//
2018/08/28 - contributed by Glen Fraser
//
- added basic test
#
define
DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN
#
include
<
doctest.h
>
#
include
<
taskflow/threadpool/threadpool_cxx14.hpp
>
#
include
<
vector
>
#
include
<
utility
>
#
include
<
chrono
>
#
include
<
limits.h
>
//
--------------------------------------------------------
//
Testcase: Threadpool.Basic
//
--------------------------------------------------------
TEST_CASE
(
"
Threadpool.Basic
"
*
doctest::timeout
(
5
)) {
using
namespace
std
::chrono_literals
;
size_t
num_workers =
4
;
size_t
num_tasks =
1000
;
tf::Threadpool
tp
(
static_cast
<
unsigned
>(num_workers));
REQUIRE
(tp.
num_workers
() == num_workers);
std::atomic<
int
> counter {
0
};
std::vector<std::future<
void
>> void_tasks;
for
(
size_t
i=
0
;i<num_tasks;i++){
void_tasks.
emplace_back
(tp.
async
([&counter, &tp]() {
REQUIRE
(tp.
is_worker
());
counter +=
1
;
}));
}
REQUIRE
(!tp.
is_worker
());
{
auto
& fu = void_tasks.
front
();
REQUIRE
(fu.
wait_for
(1s) == std::future_status::ready);
REQUIRE
(tp.
num_tasks
() < num_tasks);
}
for
(
auto
& fu : void_tasks) {
fu.
get
();
}
REQUIRE
(tp.
num_tasks
() ==
0
);
REQUIRE
(counter == num_tasks);
tp.
spawn
(
1
);
REQUIRE
(tp.
num_workers
() == num_workers +
1
);
tp.
shutdown
();
REQUIRE
(tp.
num_workers
() ==
0
);
tp.
spawn
(
static_cast
<
unsigned
>(num_workers));
REQUIRE
(tp.
num_workers
() == num_workers);
}
//
--------------------------------------------------------
//
Testcase: Threadpool.WaitForAll
//
--------------------------------------------------------
TEST_CASE
(
"
Threadpool.WaitForAll
"
*
doctest::timeout
(
5
)) {
using
namespace
std
::chrono_literals
;
size_t
num_workers =
4
;
size_t
num_tasks =
100
;
tf::Threadpool
tp
(
static_cast
<
unsigned
>(num_workers));
std::atomic<
int
> counter{
0
};
std::vector<std::future<
void
>> void_tasks;
for
(
size_t
i =
0
; i<num_tasks; i++) {
void_tasks.
emplace_back
(tp.
async
([=, &counter]() {
std::this_thread::sleep_for
((num_tasks - i) * 1ms);
++counter;
}));
}
REQUIRE
(void_tasks.
front
().
wait_for
(1s) == std::future_status::ready);
REQUIRE
(void_tasks.
back
().
wait_for
(0s) != std::future_status::ready);
tp.
wait_for_all
();
//
Last task should be done by now...
REQUIRE
(void_tasks.
back
().
wait_for
(0s) == std::future_status::ready);
//
In fact, all tasks should be done!
for
(
auto
& fu : void_tasks) {
REQUIRE
(fu.
wait_for
(0s) == std::future_status::ready);
}
REQUIRE
(tp.
num_tasks
() ==
0
);
REQUIRE
(counter == num_tasks);
}
Back
|
FazBrowse Home
|
New Git URL