-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathexample.cpp
55 lines (50 loc) · 1.54 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
// Include this first to check for missed dependencies.
#include "threadpool.hpp"
#include "latch.hpp"
#if (!defined(__MINGW32__) || defined(_GLIBCXX_HAS_GTHREADS))
#include <thread>
#else
#include <mingw.thread.h>
#endif
#include <iostream>
#include <atomic>
#include <chrono>
int main()
{
ThreadPool pool;
latch continuation_guard (1024);
std::atomic<int> counter {0};
// To wait without blocking threads, define a continuation function, to be
// executed after all other tasks complete.
std::function<void(void)> continuation = [&]()
{
// Check whether all other subtasks are complete. If not, push another
// call to this continuation function into the pool.
if (!continuation_guard.try_wait())
{
pool.schedule(continuation);
return;
}
if (counter.load(std::memory_order_relaxed) == 1024)
std::cout << "SUCCESS\n";
else
std::cout << "FAILED\n";
};
pool.schedule([&](){
for (int j = 0; j < 1024; ++j)
{
pool.schedule_subtask([&](){
std::this_thread::sleep_for(std::chrono::milliseconds(5));
counter.fetch_add(1, std::memory_order_relaxed);
continuation_guard.count_down(1);
});
}
pool.schedule(continuation);
});
// Threads outside the pool can take a simpler approach, using the OS's
// preemptive scheduling or other waiting mechanisms.
continuation_guard.wait();
std::cout << "Finishing.\n";
std::this_thread::sleep_for(std::chrono::milliseconds(200));
return 0;
}