34 template<
typename result_t =
void>
37 template<
typename result_t>
58 template<
typename result_t =
void>
83 data_->parameterized_func =
func;
84 data_->state = &state;
88 data_->state = &state;
89 data_->cancellation_token = cancellation_token;
94 template<
typename create_result_t>
98 template<
typename create_result_t>
101 data_->cancellation_token = cancellation_token;
103 template<
typename create_result_t>
105 data_->parameterized_func =
func;
106 data_->state = &state;
108 template<
typename create_result_t>
110 data_->parameterized_func =
func;
111 data_->state = &state;
112 data_->cancellation_token = cancellation_token;
127 [[nodiscard]]
auto id() const noexcept ->
xtd::
usize override {
return data_->id;}
137 [[nodiscard]]
static auto completed_task() -> task<result_t>;
138 [[nodiscard]]
static auto current_id() noexcept ->
xtd::
usize {
return current_id_;}
139 [[nodiscard]]
static auto factory() noexcept -> const
xtd::threading::tasks::task_factory&;
145 auto continue_with(
xtd::action<> continuation) ->
void {
146 auto call_now =
false;
148 lock_(data_->sync_root) {
150 else data_->continuation = continuation;
153 if (call_now) continuation();
156 [[noreturn]]
auto rethrow_exception() ->
void {
157 if (is_faulted () && data_->exception)
throw *data_->exception.source_exception();
161 auto run_synchronously() ->
void {
164 data_->task_proc(data_->state,
false);
173 data_->start_event.set();
183 return wait(milliseconds_timeout, data_->cancellation_token ? *data_->cancellation_token :
cancellation_token);
210 template<
typename from_exception_t>
223 template<
typename collection_t>
226 template<
typename collection_t>
227 static auto wait_all(
const collection_t&
tasks,
xtd::int32 milliseconds_timeout) ->
bool {
228 auto task_pointers = std::vector<itask*> {};
229 for (
auto& task : tasks)
230 task_pointers.push_back(
const_cast<decltype(&task)
>(&task));
231 return wait_all(
array<itask*> {task_pointers}, milliseconds_timeout);
234 template<
typename collection_t>
235 static auto wait_all(
const collection_t& tasks,
const xtd::time_span& timeout) ->
bool {
return wait_all(tasks,
xtd::as<xtd::int32>(timeout.total_milliseconds_duration().count()));}
237 template<
typename collection_t>
240 template<
typename collection_t>
241 static xtd::usize wait_any(
const collection_t& tasks,
int32 milliseconds_timeout) {
242 auto task_pointers = std::vector<itask*> {};
243 for (
auto& task : tasks)
244 task_pointers.push_back(
const_cast<decltype(&task)
>(&task));
245 return wait_any(xtd::array<itask*> {task_pointers}, milliseconds_timeout);
248 template<
typename collection_t>
249 static xtd::usize wait_any(
const collection_t& tasks,
const time_span& timeout) {
return wait_any(tasks,
as<int32>(timeout.total_milliseconds_duration().count()));}
251 template<
typename ...items_t>
252 static auto when_all(items_t&&... items) -> task<>;
254 template<
typename ...items_t>
255 static auto when_any(items_t&&... items) -> task<xtd::usize>;
259 template<
typename ...items_t>
261 template<
typename ...items_t>
262 static auto wait_all(
const xtd::time_span& timeout, items_t... items) ->
bool {
return wait_all(
xtd::as<xtd::int32>(timeout.total_milliseconds()), items...);}
263 template<
typename ...items_t>
264 static auto wait_all(
xtd::int32 milliseconds_timeout, items_t... items) ->
bool {
265 auto task_pointers = std::vector<itask*> {};
266 fill_task_pointers(task_pointers, items...);
267 return wait_all(xtd::array<itask*> {task_pointers}, milliseconds_timeout);
269 template<
typename item_t>
270 static auto wait_all(
const std::initializer_list<item_t>& tasks) ->
bool {
return wait_all(tasks,
timeout::infinite);}
271 template<
typename item_t>
272 static auto wait_all(
const std::initializer_list<item_t>& tasks,
xtd::int32 milliseconds_timeout) ->
bool {
273 auto task_pointers = std::vector<itask*> {};
274 for (
auto& task : tasks)
275 task_pointers.push_back(
const_cast<item_t*
>(&task));
276 return wait_all(xtd::array<itask*> {task_pointers}, milliseconds_timeout);
278 template<
typename item_t>
279 static auto wait_all(
const std::initializer_list<item_t>& tasks,
const xtd::time_span& timeout) ->
bool {
return wait_all(tasks,
as<int32>(timeout.total_milliseconds_duration().count()));}
282 auto task_pointers = std::vector<itask*> {};
283 for (
auto& task : tasks)
284 task_pointers.push_back(task.get());
285 return wait_all(xtd::array<itask*> {task_pointers}, milliseconds_timeout);
287 static auto wait_all(
const std::initializer_list<
xtd::sptr<itask>>& tasks,
const xtd::time_span& timeout) ->
bool {
return wait_all(tasks,
xtd::as<xtd::int32>(timeout.total_milliseconds_duration().count()));}
290 auto task_pointers = std::vector<itask*> {};
291 for (
auto& task : tasks)
292 task_pointers.push_back(task.get());
293 return wait_all(xtd::array<itask*> {task_pointers}, milliseconds_timeout);
295 static auto wait_all(
const std::initializer_list<
xtd::uptr<itask>>& tasks,
const xtd::time_span& timeout) ->
bool {
return wait_all(tasks,
xtd::as<xtd::int32>(timeout.total_milliseconds_duration().count()));}
298 auto task_pointers = std::vector<itask*> {};
299 for (
auto& task : tasks)
300 task_pointers.push_back(task.get());
301 return wait_all(xtd::array<itask*> {task_pointers}, milliseconds_timeout);
303 static auto wait_all(
const xtd::array<
xtd::sptr<itask>>& tasks,
const xtd::time_span& timeout) ->
bool {
return wait_all(tasks,
xtd::as<xtd::int32>(timeout.total_milliseconds_duration().count()));}
306 auto task_pointers = std::vector<itask*> {};
307 for (
auto& task : tasks)
308 task_pointers.push_back(task.get());
309 return wait_all(xtd::array<itask*> {task_pointers}, milliseconds_timeout);
311 static auto wait_all(
const xtd::array<
xtd::uptr<itask>>& tasks,
const xtd::time_span& timeout) ->
bool {
return wait_all(tasks,
xtd::as<xtd::int32>(timeout.total_milliseconds_duration().count()));}
312 static auto wait_all(
const xtd::array<itask*>& tasks,
xtd::int32 milliseconds_timeout) ->
bool {
316 for (
auto task : tasks)
322 for (
auto& task : tasks)
323 if (sw.elapsed_milliseconds() > milliseconds_timeout || task->wait(milliseconds_timeout -
xtd::as<xtd::int32>(sw.elapsed_milliseconds())))
return false;
327 template<
typename ...items_t>
329 template<
typename ...items_t>
330 static auto wait_any(
const xtd::time_span& timeout, items_t... items) ->
xtd::usize {
return wait_any(
xtd::as<xtd::int32>(timeout.total_milliseconds()), items...);}
331 template<
typename ...items_t>
333 auto task_pointers = std::vector<itask*> {};
334 fill_task_pointers(task_pointers, items...);
335 return wait_any(xtd::array<itask*> {task_pointers}, milliseconds_timeout);
337 template<
typename item_t>
339 template<
typename item_t>
340 static auto wait_any(
const std::initializer_list<item_t>& tasks,
xtd::int32 milliseconds_timeout) ->
xtd::usize {
341 auto task_pointers = std::vector<itask*> {};
342 for (
auto& task : tasks)
343 task_pointers.push_back(
const_cast<item_t*
>(&task));
344 return wait_any(xtd::array<itask*> {task_pointers}, milliseconds_timeout);
346 template<
typename item_t>
347 static auto wait_any(
const std::initializer_list<item_t>& tasks,
const xtd::time_span& timeout) ->
xtd::usize {
return wait_any(tasks,
as<int32>(timeout.total_milliseconds_duration().count()));}
350 auto task_pointers = std::vector<itask*> {};
351 for (
auto& task : tasks)
352 task_pointers.push_back(task.get());
353 return wait_any(xtd::array<itask*> {task_pointers}, milliseconds_timeout);
358 auto task_pointers = std::vector<itask*> {};
359 for (
auto& task : tasks)
360 task_pointers.push_back(task.get());
361 return wait_any(xtd::array<itask*> {task_pointers}, milliseconds_timeout);
366 auto task_pointers = std::vector<itask*> {};
367 for (
auto& task : tasks)
368 task_pointers.push_back(task.get());
369 return wait_any(xtd::array<itask*> {task_pointers}, milliseconds_timeout);
374 auto task_pointers = std::vector<itask*> {};
375 for (
auto& task : tasks)
376 task_pointers.push_back(task.get());
377 return wait_any(xtd::array<itask*> {task_pointers}, milliseconds_timeout);
380 static auto wait_any(
const xtd::array<itask*>& tasks,
xtd::int32 milliseconds_timeout) ->
xtd::usize {
383 auto sleep_duration = 1;
384 const auto max_sleep = 10;
388 for (
auto index =
xtd::usize {0}; index < tasks.length(); ++index)
389 if (tasks[index]->
wait(0))
return index;
392 sleep_duration = std::min(sleep_duration + 1, max_sleep);
398 static auto yield() -> task<result_t>;
402 template<
typename task_result_t>
404 template<
typename batic_task_result_t>
405 friend class batic_task;
413 [[nodiscard]]
auto async_wait_handle() noexcept -> xtd::threading::wait_handle&
override {
return data_->async_event;}
416 template<
typename item_t,
typename ...items_t>
417 static auto fill_task_pointers(std::vector<itask*>& itask_pointer, item_t&
first, items_t& ... rest) ->
void {
418 itask_pointer.push_back(&
first);
419 fill_task_pointers(itask_pointer, rest...);
421 template<
typename item_t>
422 static auto fill_task_pointers(std::vector<itask*>& itask_pointer, item_t& item) ->
void {
423 itask_pointer.push_back(&item);
427 using result_type = std::conditional_t<std::is_same_v<result_t, void>, std::uint8_t, result_t>;
430 xtd::threading::manual_reset_event async_event;
431 xtd::any_object async_state;
433 xtd::action<> continuation;
435 const xtd::any_object empty_state;
436 xtd::threading::auto_reset_event end_event;
437 xtd::exception_services::exception_dispatch_info exception;
443 const xtd::any_object* state = &empty_state;
444 xtd::threading::auto_reset_event start_event;
446 xtd::object sync_root;
449 previous_current_id = current_id_;
454 if (!continuation.is_empty()) continuation();
455 current_id_ = previous_current_id;
461 if constexpr(std::is_same_v<result_t, void>) {
462 if (!func.is_empty())
func();
463 else if (!parameterized_func.is_empty()) parameterized_func(state);
465 if (!func.is_empty()) result =
func();
466 else if (!parameterized_func.is_empty()) result = parameterized_func(state);
470 }
catch (
const xtd::threading::tasks::task_canceled_exception&
e) {
473 }
catch (
const xtd::exception&
e) {
476 }
catch (
const std::exception&
e) {
Contains xtd::action delegate.
Contains xtd::aggregate_exception exception.
Contains xtd::threading::auto_reset_event exception.
Represent a polymorphic wrapper capable of holding any type.
Definition any_object.hpp:29
Provides methods for creating, manipulating, searching, and sorting arrays, thereby serving as the ba...
Definition array.hpp:64
static constexpr xtd::usize max_value
Definition box_integer.hpp:71
static auto start_new() noexcept -> xtd::diagnostics::stopwatch
Initializes a new xtd::diagnostics::stopwatch instance, sets the xtd::diagnostics::stopwatch::elapsed...
static auto capture() -> exception_dispatch_info
Creates an xtd::exception_services::exception_dispatch_info object that represents the specified exce...
Definition exception_dispatch_info.hpp:70
Defines the base class for predefined exceptions in the xtd namespace.
Definition exception.hpp:29
exception(const xtd::diagnostics::stack_frame &stack_frame=xtd::diagnostics::stack_frame::current())
Create a new instance of class exception.
static auto throws(xtd::helpers::exception_case exception_case, const source_location &location=source_location::current()) -> void
Throws an exption with specified exception case.
Represents the status of an asynchronous operation.
Definition iasync_result.hpp:25
virtual auto is_completed() const noexcept -> bool=0
Gets a value that indicates whether the asynchronous operation has completed.
virtual auto async_wait_handle() noexcept -> xtd::threading::wait_handle &=0
Gets a xtd::threading::wait_handle that is used to wait for an asynchronous operation to complete.
virtual auto completed_synchronously() const noexcept -> bool=0
Gets a value that indicates whether the asynchronous operation completed synchronously.
Propagates notification that operations should be canceled.
Definition cancellation_token.hpp:39
auto wait_handle() noexcept -> threading::wait_handle &
Gets a xtd::threading::wait_handle that is signaled when the token is canceled.
auto wait() -> void override
Waits for the xtd::threading::tasks::task to complete execution.
Definition basic_task.hpp:177
auto wait(const xtd::time_span &timeout) -> bool override
Waits for the xtd::threading::tasks::task to complete execution with specified timout.
Definition basic_task.hpp:198
basic_task(const xtd::func< result_t > &func)
Initializes a new xtd::threading::tasks::basic_task <result_t> with the specified function.
Definition basic_task.hpp:75
auto is_completed() const noexcept -> bool override
Gets a value that indicates whether the asynchronous operation has completed.
Definition basic_task.hpp:129
auto async_state() const noexcept -> xtd::any_object override
Gets a user-defined object that qualifies or contains information about an asynchronous operation.
Definition basic_task.hpp:120
static constexpr xtd::usize wait_timeout
Indicates that a xtd::threading::wait_handle::wait_any operation timed out before any of the wait han...
Definition basic_task.hpp:68
auto wait(const xtd::time_span &timeout, xtd::threading::cancellation_token &cancellation_token) -> bool override
Waits for the xtd::threading::tasks::task to complete execution with specified timout and cancellatio...
Definition basic_task.hpp:202
auto wait(xtd::int32 milliseconds_timeout) -> bool override
Waits for the xtd::threading::tasks::task to complete execution with specified milliseconds timout.
Definition basic_task.hpp:181
auto start() -> void override
Starts the xtd::threading::tasks::task, scheduling it for execution to the current xtd::threading::ta...
Definition basic_task.hpp:168
auto id() const noexcept -> xtd::usize override
Gets an ID for this xtd::threading::tasks::task instance.
Definition basic_task.hpp:127
auto wait(xtd::int32 milliseconds_timeout, xtd::threading::cancellation_token &cancellation_token) -> bool override
Waits for the xtd::threading::tasks::task to complete execution with specified milliseconds timout an...
Definition basic_task.hpp:188
Represents an interface for an asynchronous operation.
Definition itask.hpp:19
virtual auto wait() -> void=0
Waits for the task to complete execution.
Provides support for creating and scheduling Task objects.
Definition task_factory.hpp:19
Represents an asynchronous operation object.
Definition task_object.hpp:28
Represents an asynchronous operation.
Definition task_result.hpp:20
static auto register_wait_for_single_object(wait_handle &wait_object, const wait_or_timer_callback &callback, const xtd::any_object &state, int32 milliseconds_timeout_interval, bool execute_only_once) -> registered_wait_handle
Registers a delegate to wait for a xtd::threading::wait_handle, specifying a 32-bit signed integer fo...
static auto sleep(int32 milliseconds_timeout) -> void
Suspends the current thread for a specified time.
Contains a constant used to specify an infinite amount of time. This class cannot be inherited.
Definition timeout.hpp:33
static constexpr int32 infinite
A constant used to specify an infinite waiting period. This field is constant.
Definition timeout.hpp:41
static auto wait_any(const collection_t &wait_handles) -> xtd::usize
Waits for any of the elements in the specified collection to receive a signal.
Definition wait_handle.hpp:198
Represents a time interval.
Definition time_span.hpp:29
Contains xtd::exception_services::exception_dispatch_info class.
Contains xtd::func delegate.
xtd::delegate< result_t(arguments_t... arguments)> func
Represents a delegate that has variables parameters and returns a value of the type specified by the ...
Definition func.hpp:27
xtd::delegate< void(const xtd::any_object &state, bool timed_out)> wait_or_timer_callback
Represents a method to be called when a xtd::threading::wait_handle is signaled or times out.
Definition wait_or_timer_callback.hpp:30
xtd::delegate< void(async_result ar)> async_callback
References a method to be called when a corresponding asynchronous operation completes.
Definition delegate.hpp:39
@ argument
The argument is not valid.
Definition exception_case.hpp:31
@ task_canceled
The task is canceled.
Definition exception_case.hpp:103
@ invalid_operation
The operation is not valid.
Definition exception_case.hpp:65
#define scope_exit_
Nowadays, every C++ developer is familiar with the Resource Acquisition Is Initialization (RAII) tech...
Definition scope_exit.hpp:128
#define delegate_
The declaration of a delegate type is similar to a method signature. It has a return value and any nu...
Definition delegate.hpp:1018
#define lock_(object)
The lock_ keyword marks a statement block as a critical section by obtaining the mutual-exclusion loc...
Definition lock.hpp:68
task_status
Represents the current stage in the lifecycle of a xtd::threading::tasks::task.
Definition task_status.hpp:23
task_creation_options
Specifies flags that control optional behavior for the creation and execution of tasks....
Definition task_creation_options.hpp:23
@ waiting_to_run
The task has been scheduled for execution but has not yet begun executing.
Definition task_status.hpp:29
@ running
The task is running but has not yet completed.
Definition task_status.hpp:31
@ ran_to_completion
The task completed execution successfully.
Definition task_status.hpp:35
@ waiting_for_activation
The task is waiting to be activated and scheduled internally by the Switch infrastructure.
Definition task_status.hpp:27
@ faulted
The task completed due to an unhandled exception.
Definition task_status.hpp:39
@ canceled
The task acknowledged cancellation by throwing an OperationCanceledException with its own Cancellatio...
Definition task_status.hpp:37
@ created
The task has been initialized but has not yet been scheduled.
Definition task_status.hpp:25
@ none
Specifies that the default behavior should be used.
Definition task_creation_options.hpp:25
auto yield() noexcept -> bool
Suggests that the implementation reschedule execution of threads.
xtd::shared_ptr_object< type_t > sptr
The xtd::sptr object is a shared pointer.
Definition sptr.hpp:25
null_ptr null
Represents a null pointer value.
xtd::unique_ptr_object< type_t > uptr
The xtd::uptr object is a unique pointer.
Definition uptr.hpp:25
std::int32_t int32
Represents a 32-bit signed integer.
Definition int32.hpp:25
xtd::reference_wrapper_object< type_t > ref
The xtd::ref object is a reference wrapper.
Definition ref.hpp:25
std::optional< type_t > optional
Represents the optional alias on std::optional.
Definition optional.hpp:26
std::size_t usize
Represents an unsigned size of any object in bytes.
Definition usize.hpp:22
auto as(any_object &o) -> type_t
Casts a type into another type.
Definition __as_any_object.hpp:60
auto as< xtd::int32 >(xtd::any value) -> xtd::int32
Casts a type into another type.
Definition __as_int32.hpp:36
auto new_sptr(args_t &&... args) -> xtd::sptr< type_t >
xtd::new_sptr operator creates a xtd::sptr object.
Definition new_sptr.hpp:24
@ e
The E key.
Definition console_key.hpp:96
Contains xtd::iasync_result interface.
Contains xtd::threading::tasks::itask interface.
The xtd::threading::tasks namespace provides types that simplify the work of writing concurrent and a...
Definition basic_task.hpp:32
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
auto first() const -> read_only_span< type_t, count >
Obtains a subspan consisting of the first count elements of the sequence.
Definition read_only_span.hpp:249
Contains xtd::new_sptr method.
Contains xtd::optional type.
Contains scope_exit_ keyword.
Contains xtd::diagnostics::stopwatch class.
Contains xtd::threading::tasks::task_canceled_exception exception.
Contains xtd::threading::tasks::task_creation_options enum class.
Contains xtd::threading::tasks::task_object class.
Contains xtd::threading::tasks::task_status enum class.
Contains xtd::threading::thread_pool class.
Contains xtd::threading::lock class.
Contains xtd::helpers::throw_helper class.