CodePorting.Translator Cs2Cpp
CodePorting.Translator.Cs2Cpp.Framework
parallel.h
1
4
5#pragma once
6
7#include <system/action.h>
8#include <system/func.h>
9#include <system/object.h>
10#include <system/threading/tasks/task_utils.h>
11
12namespace System { namespace Threading { namespace Tasks {
13
17class ASPOSECPP_SHARED_CLASS ParallelLoopResult : public Object, public System::Details::BoxableObjectBase
18{
20 typedef Object BaseType;
21
22 typedef ::System::BaseTypesInfo<BaseType> ThisTypeBaseTypesInfo;
23
24public:
26 ParallelLoopResult() : _completed(false) {}
27
30 bool get_IsCompleted() {return _completed;}
31
34 Nullable<int64_t> get_LowestBreakIteration() {return _lowestBreakIteration;}
35
36protected:
39};
40
43class ASPOSECPP_SHARED_CLASS ParallelOptions : public Object
44{
46 typedef Object BaseType;
47
48 typedef ::System::BaseTypesInfo<BaseType> ThisTypeBaseTypesInfo;
49
50public:
54 ASPOSECPP_SHARED_API ParallelOptions();
55
58 const SharedPtr<TaskScheduler>& get_TaskScheduler() {return _scheduler;}
59
62 void set_TaskScheduler(const SharedPtr<TaskScheduler>& value) {_scheduler = value;}
63
66 int32_t get_MaxDegreeOfParallelism() {return _maxDegreeOfParallelism;}
67
70 void set_MaxDegreeOfParallelism(int32_t value) {_maxDegreeOfParallelism = value;}
71
74 const CancellationToken& get_CancellationToken() {return _cancellationToken;}
75
78 void set_CancellationToken(const CancellationToken& value) {_cancellationToken = value;}
79
80protected:
81 friend class Parallel;
82
86
89 ASPOSECPP_SHARED_API int32_t get_EffectiveMaxConcurrencyLevel();
90
91private:
92 SharedPtr<TaskScheduler> _scheduler;
93 int32_t _maxDegreeOfParallelism;
94 CancellationToken _cancellationToken;
95};
96
100class ASPOSECPP_SHARED_CLASS Parallel
101{
102public:
111 template <typename TSource>
113 const SharedPtr<ParallelOptions>& parallelOptions, const Action<TSource>& body)
114 {
115 parallelOptions->get_CancellationToken().ThrowIfCancellationRequested();
116
117 auto enumerator = source->GetEnumerator();
118 auto tasks = MakeArray<TaskPtr>(parallelOptions->get_EffectiveMaxConcurrencyLevel());
119 auto lock = MakeObject<Object>();
120
121 for (auto& task : tasks)
122 {
123 task = MakeAsync([=, &lock, &enumerator, &body](System::Details::AsyncContext& __) mutable
124 {
125 if (parallelOptions->get_CancellationToken().get_IsCancellationRequested())
126 {
127 __.Return(); return;
128 }
129
130 TSource value;
131 {
132 LockContext __guard((lock).GetPointer());
133 if (enumerator->MoveNext())
134 {
135 value = enumerator->get_Current();
136 }
137 else
138 {
139 __.Return(); return;
140 }
141 }
142 body(value);
143 });
144 }
145
146 Tasks::WaitAll(tasks, parallelOptions->get_CancellationToken());
147
148 return ParallelLoopResult();
149 }
150
157 template<typename TSource>
159 {
160 return ForEach(source, s_defaultParallelOptions, body);
161 }
162
163protected:
167};
168
169}}} // namespace System::Threading::Tasks
Interface of object providing enumerator on contained elements.
Definition: ienumerable.h:25
Base class that enables using methods available for System.Object class in C#. All non-trivial classe...
Definition: object.h:62
Pointer class to wrap types being allocated on heap. Use it to manage memory for classes inheriting O...
Definition: smart_ptr.h:180
Propagates notification that operations should be canceled. This class provides a mechanism for coope...
Definition: cancellation_token.h:43
Provides support for parallel loops and regions.
Definition: parallel.h:101
static ParallelLoopResult ForEach(const SharedPtr< Collections::Generic::IEnumerable< TSource > > &source, const Action< TSource > &body)
Executes a foreach operation on an IEnumerable in which iterations may run in parallel.
Definition: parallel.h:158
static ParallelLoopResult ForEach(const SharedPtr< Collections::Generic::IEnumerable< TSource > > &source, const SharedPtr< ParallelOptions > &parallelOptions, const Action< TSource > &body)
Executes a foreach operation on an IEnumerable in which iterations may run in parallel.
Definition: parallel.h:112
static SharedPtr< ParallelOptions > s_defaultParallelOptions
Default ParallelOptions instance used when no options are specified.
Definition: parallel.h:166
Provides completion status of a Parallel loop.
Definition: parallel.h:18
bool get_IsCompleted()
Gets whether the loop ran to completion.
Definition: parallel.h:30
ParallelLoopResult()
Constructs a ParallelLoopResult.
Definition: parallel.h:26
bool _completed
Indicates if the loop completed.
Definition: parallel.h:37
Nullable< int64_t > get_LowestBreakIteration()
Gets the lowest iteration index at which a break occurred.
Definition: parallel.h:34
Nullable< int64_t > _lowestBreakIteration
Stores the lowest break iteration index.
Definition: parallel.h:38
Stores options that configure the operation of methods on the Parallel class.
Definition: parallel.h:44
int32_t get_MaxDegreeOfParallelism()
Gets the maximum degree of parallelism enabled by this ParallelOptions instance.
Definition: parallel.h:66
void set_MaxDegreeOfParallelism(int32_t value)
Sets the maximum degree of parallelism enabled by this ParallelOptions instance.
Definition: parallel.h:70
int32_t get_EffectiveMaxConcurrencyLevel()
Gets the effective maximum concurrency level.
void set_TaskScheduler(const SharedPtr< TaskScheduler > &value)
Sets the TaskScheduler associated with this ParallelOptions instance.
Definition: parallel.h:62
void set_CancellationToken(const CancellationToken &value)
Sets the CancellationToken associated with this ParallelOptions instance.
Definition: parallel.h:78
ParallelOptions()
Constructs a ParallelOptions instance with default values.
const SharedPtr< TaskScheduler > & get_TaskScheduler()
Gets the TaskScheduler associated with this ParallelOptions instance.
Definition: parallel.h:58
SharedPtr< TaskScheduler > get_EffectiveTaskScheduler()
Gets the effective TaskScheduler to use.
const CancellationToken & get_CancellationToken()
Gets the CancellationToken associated with this ParallelOptions instance.
Definition: parallel.h:74
void WaitAll(const ArrayPtr< TaskPtr > &tasks, const CancellationToken &cancellationToken)
Waits for all of the provided Task objects to complete execution.
Definition: db_command.h:9
TaskPtr MakeAsync(const Details::AsyncFunction &fnc)
MulticastDelegate< void(Args...)> Action
Delegate type that references methods that have no return value.
Definition: action.h:40