forked from taskflow/taskflow
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paththreadpool_cxx14.cpp
More file actions
59 lines (43 loc) · 1.32 KB
/
threadpool_cxx14.cpp
File metadata and controls
59 lines (43 loc) · 1.32 KB
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
// 2018/08/28 - contributed by Glen Fraser
#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
// --------------------------------------------------------
TEST_CASE("Threadpool" * 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(num_workers);
REQUIRE(tp.num_workers() == num_workers);
}