xtd 1.0.0
Loading...
Searching...
No Matches
parallel.hpp
Go to the documentation of this file.
1
4#pragma once
5#include "task_factory.hpp"
8#include "../../array.hpp"
9#include "../../integer.hpp"
10#include "../../static.hpp"
11
13namespace xtd {
15 namespace threading {
17 namespace tasks {
25 public:
26 template<xtd::integer integer_t>
27 static auto for_(integer_t from_inclusive, integer_t to_exclusive, auto&& body) -> void {
28 for_each(xtd::linq::enumerable::range(from_inclusive, to_exclusive - from_inclusive), body);
29 }
30
31 template<xtd::integer integer_t>
32 static auto for_(integer_t from_inclusive, integer_t to_exclusive, auto&& body, const xtd::threading::cancellation_token& cancellation_token) -> void {
33 for_each(xtd::linq::enumerable::range(from_inclusive, to_exclusive - from_inclusive), body, cancellation_token).wait();
34 }
35
36 template<xtd::integer integer_t>
37 static auto for_async(integer_t from_inclusive, integer_t to_exclusive, auto&& body) -> xtd::threading::tasks::task<> {
38 return for_each_async(xtd::linq::enumerable::range(from_inclusive, to_exclusive - from_inclusive), body);
39 }
40
41 template<xtd::integer integer_t>
42 static auto for_async(integer_t from_inclusive, integer_t to_exclusive, auto&& body, const xtd::threading::cancellation_token& cancellation_token) -> xtd::threading::tasks::task<> {
43 return for_each_async(xtd::linq::enumerable::range(from_inclusive, to_exclusive - from_inclusive), body, cancellation_token);
44 }
45
46 template<xtd::iterable source_t>
47 static auto for_each(source_t&& source, auto&& body) -> void {
48 for_each_async(std::forward<source_t>(source), body).wait();
49 }
50
51 template<xtd::iterable source_t>
52 static auto for_each(source_t&& source, auto&& body, const xtd::threading::cancellation_token& cancellation_token) -> void {
53 for_each_async(std::forward<source_t>(source), body, cancellation_token).wait();
54 }
55
61 template<xtd::iterable source_t>
62 static auto for_each_async(source_t&& source, auto&& body) -> xtd::threading::tasks::task<> {
63 using result_t = xtd::raw_type<decltype(body(std::declval<const xtd::iterable_value_type<source_t>&>()))>;
65
66 for(const auto& item : source)
67 tasks.add(xtd::threading::tasks::task<>::factory().start_new([body](auto state) -> result_t {return body(as<xtd::iterable_value_type<source_t>>(state));}, xtd::any_object(item)));
68
70 }
71
78 template<xtd::iterable source_t>
80 using result_t = xtd::raw_type<decltype(body(std::declval<const xtd::iterable_value_type<source_t>&>()))>;
82
83 for(const auto& item : source)
84 tasks.add(xtd::threading::tasks::task<>::factory().start_new([body](auto state) -> result_t {return body(as<xtd::iterable_value_type<source_t>>(state));}, xtd::any_object(item), cancellation_token));
85
87 }
88
90 template<typename type_t>
91 static auto for_each(std::initializer_list<type_t>&& source, auto&& body) -> void {
92 for_each(xtd::collections::generic::list<type_t>(source), body);
93 }
94
95 template<typename type_t>
96 static auto for_each(std::initializer_list<type_t>&& source, auto&& body, const xtd::threading::cancellation_token& cancellation_token) -> void {
98 }
99
100 template<typename type_t>
101 static auto for_each_async(std::initializer_list<type_t>&& source, auto&& body) -> xtd::threading::tasks::task<> {
103 }
104
105 template<typename type_t>
106 static auto for_each_async(std::initializer_list<type_t>&& source, auto&& body, const xtd::threading::cancellation_token& cancellation_token) -> xtd::threading::tasks::task<> {
107 return for_each_async(xtd::collections::generic::list<type_t>(source), body, cancellation_token);
108 }
110
121 static auto invoke(xtd::array<xtd::action<>>&& actions) -> void {
122 task<>::wait_all(invoke_async(actions));
123 }
124
134 template<typename collection_t>
135 static auto invoke(collection_t&& actions) -> void {
136 task<>::wait_all(invoke_async(actions));
137 }
138
148 template<typename ...args_t>
149 static auto invoke(args_t&&... args) -> void {invoke(xtd::array<xtd::action<>> {xtd::action<> {std::forward<args_t>(args)}...});}
150
151 static auto invoke_async(xtd::array<xtd::action<>>&& actions) -> xtd::array<xtd::threading::tasks::task<>> {
153 for (const auto& action : actions)
154 tasks.add(xtd::threading::tasks::task<>::factory().start_new(action));
155 return tasks.to_array();
156 }
157
158 template<typename collection_t>
159 static auto invoke_async(collection_t&& actions) -> xtd::array<xtd::threading::tasks::task<>> {
161 for (const auto& action : actions)
162 tasks.add(xtd::threading::tasks::task<>::factory().start_new(action));
163 return tasks.to_array();
164 }
165
166 template<typename ...args_t>
167 static auto invoke_async(args_t&&... args) -> xtd::array<xtd::threading::tasks::task<>> {return invoke_async(xtd::array<xtd::action<>> {xtd::action<> {std::forward<args_t>(args)}...});}
168 };
169 }
170 }
171}
Contains xtd::action_callable concept.
Contains xtd::array class.
Represent a polymorphic wrapper capable of holding any type.
Definition any_object.hpp:30
Provides methods for creating, manipulating, searching, and sorting arrays, thereby serving as the ba...
Definition array.hpp:64
Represents a strongly typed list of objects that can be accessed by index. Provides methods to search...
Definition list.hpp:84
static auto range(integer_t count) -> xtd::collections::generic::enumerable_generator< integer_t >
Generates a sequence of integral numbers within a specified range.
Propagates notification that operations should be canceled.
Definition cancellation_token.hpp:39
Provides support for creating and scheduling Task objects.
Definition parallel.hpp:24
static auto for_each_async(source_t &&source, auto &&body) -> xtd::threading::tasks::task<>
Executes a for-each operation on an xtd::iterable in which iterations may run in parallel.
Definition parallel.hpp:62
static auto invoke(collection_t &&actions) -> void
Executes each of the provided actions, possibly in parallel.
Definition parallel.hpp:135
static auto for_each_async(source_t &&source, auto &&body, const xtd::threading::cancellation_token &cancellation_token) -> xtd::threading::tasks::task<>
Executes a for-each operation on an xtd::iterable in which iterations may run in parallel.
Definition parallel.hpp:79
static auto invoke(args_t &&... args) -> void
Executes each of the provided actions, possibly in parallel.
Definition parallel.hpp:149
static auto invoke(xtd::array< xtd::action<> > &&actions) -> void
Executes each of the provided actions, possibly in parallel.
Definition parallel.hpp:121
Represents an asynchronous operation.
Definition task_result.hpp:20
xtd::raw_type< decltype(*std::begin(std::declval< iterable_t & >()))> iterable_value_type
Represents the orward iterable value type.
Definition iterable_value_type.hpp:29
#define static_
This keyword is use to represent a static object. A static object can't be instantiated (constructors...
Definition static.hpp:38
std::remove_cvref_t< value_t > raw_type
Represents a raw type alias equivalent to std::remove_cvref_t<value_t>.
Definition raw_type.hpp:25
auto as(any_object &o) -> type_t
Casts a type into another type.
Definition __as_any_object.hpp:60
Contains xtd::integer concept.
Contains xtd::collections::generic::list <type_t> class.
The xtd::threading::tasks namespace provides types that simplify the work of writing concurrent and a...
Definition itask.hpp:14
The xtd::threading namespace provides classes and interfaces that enable multithreaded programming....
Definition abandoned_mutex_exception.hpp:10
The xtd namespace contains all fundamental classes to access Hardware, Os, System,...
Definition abstract_object.hpp:8
Contains xtd::static_object class.
Contains xtd::threading::tasks::task_factory <result_t> class.