MCPcopy Create free account
hub / github.com/boostorg/asio / thread_executor

Class thread_executor

example/cpp14/executors/pipeline.cpp:19–75  ·  view source on GitHub ↗

An executor that launches a new thread for each function submitted to it. This class satisfies the executor requirements.

Source from the content-addressed store, hash-verified

17// An executor that launches a new thread for each function submitted to it.
18// This class satisfies the executor requirements.
19class thread_executor
20{
21private:
22 // Singleton execution context that manages threads launched by the new_thread_executor.
23 class thread_bag
24 {
25 friend class thread_executor;
26
27 void add_thread(std::thread&& t)
28 {
29 std::unique_lock<std::mutex> lock(mutex_);
30 threads_.push_back(std::move(t));
31 }
32
33 thread_bag() = default;
34
35 ~thread_bag()
36 {
37 for (auto& t : threads_)
38 t.join();
39 }
40
41 std::mutex mutex_;
42 std::vector<std::thread> threads_;
43 };
44
45public:
46 static thread_bag& query(execution::context_t)
47 {
48 static thread_bag threads;
49 return threads;
50 }
51
52 static constexpr auto query(execution::blocking_t)
53 {
54 return execution::blocking.never;
55 }
56
57 template <class Func>
58 void execute(Func f) const
59 {
60 thread_bag& bag = query(execution::context);
61 bag.add_thread(std::thread(std::move(f)));
62 }
63
64 friend bool operator==(const thread_executor&,
65 const thread_executor&) noexcept
66 {
67 return true;
68 }
69
70 friend bool operator!=(const thread_executor&,
71 const thread_executor&) noexcept
72 {
73 return false;
74 }
75};
76

Callers 1

pipelineFunction · 0.70

Calls

no outgoing calls

Tested by

no test coverage detected