xtd 0.2.0
Loading...
Searching...
No Matches
basic_task.hpp
Go to the documentation of this file.
1
4#pragma once
5#include "itask.hpp"
8#include "task_status.hpp"
10#include "../lock.hpp"
11#include "../thread_pool.hpp"
15#include "../../action.hpp"
17#include "../../func.hpp"
19#include "../../new_sptr.hpp"
20#include "../../optional.hpp"
21#include "../../ref.hpp"
22#include "../../scope_exit.hpp"
23#include "../../sptr.hpp"
24#include <coroutine>
25
27namespace xtd {
29 namespace threading {
31 namespace tasks {
33 template<class result_t = void>
34 class task;
35
36 template<class result_t>
37 struct task_awaiter;
38
39 class task_factory;
41
48 template<class result_t = void>
49 class basic_task : public itask, public xtd::iasync_result {
50 public:
51 struct yield_awaiter;
52
54
58 static constexpr xtd::size wait_timeout = xtd::size_object::max_value;
61
63 basic_task(const xtd::func<result_t>& func) {
64 data_->func = func;
65 }
66 basic_task(const xtd::func<result_t>& func, const xtd::threading::cancellation_token& cancellation_token) {
67 data_->func = func;
68 data_->cancellation_token = cancellation_token;
69 }
70 basic_task(const xtd::func<result_t, const xtd::any_object&>& func, const xtd::any_object& state) {
71 data_->parameterized_func = func;
72 data_->state = &state;
73 }
74 basic_task(const xtd::func<result_t>& func, const xtd::any_object& state, const xtd::threading::cancellation_token& cancellation_token) {
75 data_->func = func;
76 data_->state = &state;
77 data_->cancellation_token = cancellation_token;
78 }
80
82 basic_task(const std::function<void()>& func) {
83 data_->func = func;
84 }
85 basic_task(const std::function<void()>& func, const xtd::threading::cancellation_token& cancellation_token) {
86 data_->func = func;
87 data_->cancellation_token = cancellation_token;
88 }
89 basic_task(const std::function<void(const xtd::any_object&)>& func, const xtd::any_object& state) {
90 data_->parameterized_func = func;
91 data_->state = &state;
92 }
93 basic_task(const std::function<void(const xtd::any_object&)>& func, const xtd::any_object& state, const xtd::threading::cancellation_token& cancellation_token) {
94 data_->parameterized_func = func;
95 data_->state = &state;
96 data_->cancellation_token = cancellation_token;
97 }
98
99 template<class create_result_t>
100 basic_task(const std::function<create_result_t()>& func) {
101 data_->func = func;
102 }
103 template<class create_result_t>
104 basic_task(const std::function<create_result_t()>& func, const xtd::threading::cancellation_token& cancellation_token) {
105 data_->func = func;
106 data_->cancellation_token = cancellation_token;
107 }
108 template<class create_result_t>
109 basic_task(const std::function<create_result_t(const xtd::any_object&)>& func, const xtd::any_object& state) {
110 data_->parameterized_func = func;
111 data_->state = &state;
112 }
113 template<class create_result_t>
114 basic_task(const std::function<create_result_t(const xtd::any_object&)>& func, const xtd::any_object& state, const xtd::threading::cancellation_token& cancellation_token) {
115 data_->parameterized_func = func;
116 data_->state = &state;
117 data_->cancellation_token = cancellation_token;
118 }
120
121
123
125 [[nodiscard]] auto async_state() const noexcept -> xtd::any_object override {return data_->async_state;}
126 [[nodiscard]] auto creation_options() const noexcept -> xtd::threading::tasks::task_creation_options {return data_->creation_options;}
127 [[nodiscard]] auto exception() const noexcept -> xtd::ref<xtd::exception> {return data_->exception.exception_captured() ? xtd::ref<xtd::exception> {*data_->exception.source_exception()} : xtd::ref<xtd::exception> {xtd::null};}
132 [[nodiscard]] auto id() const noexcept -> xtd::size override {return data_->id;}
133 [[nodiscard]] auto is_canceled() const noexcept -> bool {return data_->status == xtd::threading::tasks::task_status::canceled;}
134 [[nodiscard]] auto is_completed() const noexcept -> bool override {return is_faulted() || is_canceled() || data_->status == xtd::threading::tasks::task_status::ran_to_completion;}
135 [[nodiscard]] auto is_faulted() const noexcept -> bool {return data_->status == xtd::threading::tasks::task_status::faulted;}
136 [[nodiscard]] auto status() const noexcept -> xtd::threading::tasks::task_status {return data_->status;}
138
140
142 [[nodiscard]] static auto completed_task() -> task<result_t>;
143 [[nodiscard]] static auto current_id() noexcept -> xtd::size {return current_id_;}
144 [[nodiscard]] static auto factory() noexcept -> const xtd::threading::tasks::task_factory&;
146
148
150 auto continue_with(std::function<void()> continuation) -> void {continue_with(xtd::action<> {continuation});}
151 auto continue_with(xtd::action<> continuation) -> void {
152 auto call_now = false;
153
154 lock_(data_->sync_root) {
155 if (is_completed()) call_now = true;
156 else data_->continuation = continuation;
157 }
158
159 if (call_now) continuation();
160 }
161
162 [[noreturn]] auto rethrow_exception() -> void {
163 if (is_faulted () && data_->exception) throw *data_->exception.source_exception();
165 }
166
167 auto run_synchronously() -> void {
169 data_->task_proc(data_->state, false);
171 }
172
174 auto start() -> void override {
175 if (is_completed()) xtd::helpers::throw_helper::throws(xtd::helpers::exception_case::invalid_operation, "Start may not be called on a task that has completed.");
177 thread_pool::register_wait_for_single_object(data_->start_event, data_->task_proc, *data_->state, xtd::threading::timeout::infinite, true);
179 data_->start_event.set();
180 }
181
183 auto wait() -> void override {wait(xtd::threading::timeout::infinite);}
187 auto wait(xtd::int32 milliseconds_timeout) -> bool override {
188 auto cancellation_token = xtd::threading::cancellation_token {};
189 return wait(milliseconds_timeout, cancellation_token);
190 }
194 auto wait(xtd::int32 milliseconds_timeout, xtd::threading::cancellation_token& cancellation_token) -> bool override {
195 auto& cancellation_token_wait_handle = cancellation_token.wait_handle();
196 auto result = xtd::threading::wait_handle::wait_any(milliseconds_timeout, &data_->end_event, &cancellation_token_wait_handle);
197 if (data_->status == xtd::threading::tasks::task_status::faulted) rethrow_exception();
198 return result;
199 }
203 auto wait(const xtd::time_span& timeout) -> bool override {return wait(xtd::as<xtd::int32>(timeout.total_milliseconds_duration().count()));}
207 auto wait(const xtd::time_span& timeout, xtd::threading::cancellation_token& cancellation_token) -> bool override {return wait(xtd::as<xtd::int32>(timeout.total_milliseconds_duration().count()), cancellation_token);}
209
211
213 [[nodiscard]] static auto from_cancelation(const xtd::threading::cancellation_token& cancellation_token) -> task<result_t>;
214
215 template<class from_exception_t>
216 [[nodiscard]] static auto from_exception(from_exception_t exception) -> task<result_t>;
217
218 [[nodiscard]] static auto delay(const xtd::time_span& delay) -> task<>;
219 [[nodiscard]] static auto delay(const xtd::time_span& delay, const xtd::threading::cancellation_token& cancellation_token) -> task<>;
220 [[nodiscard]] static auto delay(xtd::int32 milliseconds_delay) -> task<>;
221 [[nodiscard]] static auto delay(xtd::int32 milliseconds_delay, const xtd::threading::cancellation_token& cancellation_token) -> task<>;
222
223 [[nodiscard]] static auto run(const xtd::func<result_t>& func) -> task<result_t>;
224 [[nodiscard]] static auto run(const xtd::func<result_t>& func, const xtd::threading::cancellation_token& cancellation_token) -> task<result_t>;
225 [[nodiscard]] static auto run(const xtd::func<result_t, const xtd::any_object&>& func, const xtd::any_object& state) -> task<result_t>;
226 [[nodiscard]] static auto run(const xtd::func<result_t, const xtd::any_object&>& func, const xtd::any_object& state, const xtd::threading::cancellation_token& cancellation_token) -> task<result_t>;
227
228 template<class collection_t>
229 static auto wait_all(const collection_t& tasks) -> bool {return wait_all(tasks, xtd::threading::timeout::infinite);}
230
231 template<class collection_t>
232 static auto wait_all(const collection_t& tasks, xtd::int32 milliseconds_timeout) -> bool {
233 auto task_pointers = std::vector<itask*> {};
234 for (auto& task : tasks)
235 task_pointers.push_back(const_cast<decltype(&task)>(&task));
236 return wait_all(array<itask*> {task_pointers}, milliseconds_timeout);
237 }
238
239 template<class collection_t>
240 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()));}
241
242 template<class collection_t>
243 static size_t wait_any(const collection_t& tasks) {return wait_any(tasks, timeout::infinite);}
244
245 template<class collection_t>
246 static size_t wait_any(const collection_t& tasks, int32 milliseconds_timeout) {
247 auto task_pointers = std::vector<itask*> {};
248 for (auto& task : tasks)
249 task_pointers.push_back(const_cast<decltype(&task)>(&task));
250 return wait_any(xtd::array<itask*> {task_pointers}, milliseconds_timeout);
251 }
252
253 template<class collection_t>
254 static size_t wait_any(const collection_t& tasks, const time_span& timeout) {return wait_any(tasks, as<int32>(timeout.total_milliseconds_duration().count()));}
256
258 [[nodiscard]] static auto run(const std::function<result_t()>& func) -> task<result_t>;
259 [[nodiscard]] static auto run(const std::function<result_t()>& func, const xtd::threading::cancellation_token& cancellation_token) -> task<result_t>;
260 [[nodiscard]] static auto run(const std::function<result_t(const xtd::any_object&)>& func, const xtd::any_object& state) -> task<result_t>;
261 [[nodiscard]] static auto run(const std::function<result_t(const xtd::any_object&)>& func, const xtd::any_object& state, const xtd::threading::cancellation_token& cancellation_token) -> task<result_t>;
262
263 template<class ...items_t>
264 static auto wait_all(items_t... items) -> bool {return wait_all(xtd::threading::timeout::infinite, items...);}
265 template<class ...items_t>
266 static auto wait_all(const xtd::time_span& timeout, items_t... items) -> bool {return wait_all(xtd::as<xtd::int32>(timeout.total_milliseconds()), items...);}
267 template<class ...items_t>
268 static auto wait_all(xtd::int32 milliseconds_timeout, items_t... items) -> bool {
269 auto task_pointers = std::vector<itask*> {};
270 fill_task_pointers(task_pointers, items...);
271 return wait_all(xtd::array<itask*> {task_pointers}, milliseconds_timeout);
272 }
273 template<class item_t>
274 static auto wait_all(const std::initializer_list<item_t>& tasks) -> bool {return wait_all(tasks, timeout::infinite);}
275 template<class item_t>
276 static auto wait_all(const std::initializer_list<item_t>& tasks, xtd::int32 milliseconds_timeout) -> bool {
277 auto task_pointers = std::vector<itask*> {};
278 for (auto& task : tasks)
279 task_pointers.push_back(const_cast<item_t*>(&task));
280 return wait_all(xtd::array<itask*> {task_pointers}, milliseconds_timeout);
281 }
282 template<class item_t>
283 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()));}
284 static auto wait_all(const std::initializer_list<xtd::sptr<itask>>& tasks) -> bool {return wait_all(tasks, xtd::threading::timeout::infinite);}
285 static auto wait_all(const std::initializer_list<xtd::sptr<itask>>& tasks, xtd::int32 milliseconds_timeout) -> bool {
286 auto task_pointers = std::vector<itask*> {};
287 for (auto& task : tasks)
288 task_pointers.push_back(task.get());
289 return wait_all(xtd::array<itask*> {task_pointers}, milliseconds_timeout);
290 }
291 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()));}
292 static auto wait_all(const std::initializer_list<xtd::uptr<itask>>& tasks) -> bool {return wait_all(tasks, xtd::threading::timeout::infinite);}
293 static auto wait_all(const std::initializer_list<xtd::uptr<itask>>& tasks, xtd::int32 milliseconds_timeout) -> bool {
294 auto task_pointers = std::vector<itask*> {};
295 for (auto& task : tasks)
296 task_pointers.push_back(task.get());
297 return wait_all(xtd::array<itask*> {task_pointers}, milliseconds_timeout);
298 }
299 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()));}
300 static auto wait_all(const xtd::array<xtd::sptr<itask>>& tasks) -> bool {return wait_all(tasks, timeout::infinite);}
301 static auto wait_all(const xtd::array<xtd::sptr<itask>>& tasks, xtd::int32 milliseconds_timeout) -> bool {
302 auto task_pointers = std::vector<itask*> {};
303 for (auto& task : tasks)
304 task_pointers.push_back(task.get());
305 return wait_all(xtd::array<itask*> {task_pointers}, milliseconds_timeout);
306 }
307 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()));}
308 static auto wait_all(const xtd::array<xtd::uptr<itask>>& tasks) -> bool {return wait_all(tasks, timeout::infinite);}
309 static auto wait_all(const xtd::array<xtd::uptr<itask>>& tasks, xtd::int32 milliseconds_timeout) -> bool {
310 auto task_pointers = std::vector<itask*> {};
311 for (auto& task : tasks)
312 task_pointers.push_back(task.get());
313 return wait_all(xtd::array<itask*> {task_pointers}, milliseconds_timeout);
314 }
315 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()));}
316 static auto wait_all(const xtd::array<itask*>& tasks, xtd::int32 milliseconds_timeout) -> bool {
318
319 if (milliseconds_timeout == timeout::infinite) {
320 for (auto task : tasks)
321 task->wait();
322 return true;
323 }
324
326 for (auto& task : tasks)
327 if (sw.elapsed_milliseconds() > milliseconds_timeout || task->wait(milliseconds_timeout - xtd::as<xtd::int32>(sw.elapsed_milliseconds()))) return false;
328 return true;
329 }
330
331 template<class ...items_t>
332 static auto wait_any(items_t... items) -> xtd::size {return wait_any(xtd::threading::timeout::infinite, items...);}
333 template<class ...items_t>
334 static auto wait_any(const xtd::time_span& timeout, items_t... items) -> xtd::size {return wait_any(xtd::as<xtd::int32>(timeout.total_milliseconds()), items...);}
335 template<class ...items_t>
336 static auto wait_any(xtd::int32 milliseconds_timeout, items_t... items) -> xtd::size {
337 auto task_pointers = std::vector<itask*> {};
338 fill_task_pointers(task_pointers, items...);
339 return wait_any(xtd::array<itask*> {task_pointers}, milliseconds_timeout);
340 }
341 template<class item_t>
342 static auto wait_any(const std::initializer_list<item_t>& tasks) -> xtd::size {return wait_any(tasks, timeout::infinite);}
343 template<class item_t>
344 static auto wait_any(const std::initializer_list<item_t>& tasks, xtd::int32 milliseconds_timeout) -> xtd::size {
345 auto task_pointers = std::vector<itask*> {};
346 for (auto& task : tasks)
347 task_pointers.push_back(const_cast<item_t*>(&task));
348 return wait_any(xtd::array<itask*> {task_pointers}, milliseconds_timeout);
349 }
350 template<class item_t>
351 static auto wait_any(const std::initializer_list<item_t>& tasks, const xtd::time_span& timeout) -> xtd::size {return wait_any(tasks, as<int32>(timeout.total_milliseconds_duration().count()));}
352 static auto wait_any(const std::initializer_list<xtd::sptr<itask>>& tasks) -> xtd::size {return wait_any(tasks, xtd::threading::timeout::infinite);}
353 static auto wait_any(const std::initializer_list<xtd::sptr<itask>>& tasks, xtd::int32 milliseconds_timeout) -> xtd::size {
354 auto task_pointers = std::vector<itask*> {};
355 for (auto& task : tasks)
356 task_pointers.push_back(task.get());
357 return wait_any(xtd::array<itask*> {task_pointers}, milliseconds_timeout);
358 }
359 static auto wait_any(const std::initializer_list<xtd::sptr<itask>>& tasks, const xtd::time_span& timeout) -> xtd::size {return wait_any(tasks, xtd::as<xtd::int32>(timeout.total_milliseconds_duration().count()));}
360 static auto wait_any(const std::initializer_list<xtd::uptr<itask>>& tasks) -> xtd::size {return wait_any(tasks, xtd::threading::timeout::infinite);}
361 static auto wait_any(const std::initializer_list<xtd::uptr<itask>>& tasks, xtd::int32 milliseconds_timeout) -> xtd::size {
362 auto task_pointers = std::vector<itask*> {};
363 for (auto& task : tasks)
364 task_pointers.push_back(task.get());
365 return wait_any(xtd::array<itask*> {task_pointers}, milliseconds_timeout);
366 }
367 static auto wait_any(const std::initializer_list<xtd::uptr<itask>>& tasks, const xtd::time_span& timeout) -> xtd::size {return wait_any(tasks, xtd::as<xtd::int32>(timeout.total_milliseconds_duration().count()));}
368 static auto wait_any(const xtd::array<xtd::sptr<itask>>& tasks) -> xtd::size {return wait_any(tasks, timeout::infinite);}
369 static auto wait_any(const xtd::array<xtd::sptr<itask>>& tasks, xtd::int32 milliseconds_timeout) -> xtd::size {
370 auto task_pointers = std::vector<itask*> {};
371 for (auto& task : tasks)
372 task_pointers.push_back(task.get());
373 return wait_any(xtd::array<itask*> {task_pointers}, milliseconds_timeout);
374 }
375 static auto wait_any(const xtd::array<xtd::sptr<itask>>& tasks, const xtd::time_span& timeout) -> xtd::size {return wait_any(tasks, xtd::as<xtd::int32>(timeout.total_milliseconds_duration().count()));}
376 static auto wait_any(const xtd::array<xtd::uptr<itask>>& tasks) -> xtd::size {return wait_any(tasks, timeout::infinite);}
377 static auto wait_any(const xtd::array<xtd::uptr<itask>>& tasks, xtd::int32 milliseconds_timeout) -> xtd::size {
378 auto task_pointers = std::vector<itask*> {};
379 for (auto& task : tasks)
380 task_pointers.push_back(task.get());
381 return wait_any(xtd::array<itask*> {task_pointers}, milliseconds_timeout);
382 }
383 static auto wait_any(const xtd::array<xtd::uptr<itask>>& tasks, const xtd::time_span& timeout) -> xtd::size {return wait_any(tasks, xtd::as<xtd::int32>(timeout.total_milliseconds_duration().count()));}
384 static auto wait_any(const xtd::array<itask*>& tasks, xtd::int32 milliseconds_timeout) -> xtd::size {
386
387 auto sleep_duration = 1;
388 const auto max_sleep = 10;
390
391 while (milliseconds_timeout == xtd::threading::timeout::infinite || sw.elapsed_milliseconds() <= milliseconds_timeout) {
392 for (auto index = xtd::size {0}; index < tasks.length(); ++index)
393 if (tasks[index]->wait(0)) return index;
394
395 xtd::threading::thread::sleep(sleep_duration);
396 sleep_duration = std::min(sleep_duration + 1, max_sleep);
397 }
398
399 return wait_timeout;
400 }
401
402 static auto yield() -> task<result_t>;
404
405 private:
406 template<class task_result_t>
407 friend class task;
408 template<class batic_task_result_t>
409 friend class batic_task;
410
411 basic_task() = default;
412 basic_task(basic_task&&) = default;
413 basic_task(const basic_task&) = default;
414 auto operator=(basic_task&&) -> basic_task& = default;
415 auto operator=(const basic_task&) -> basic_task& = default;
416
417 [[nodiscard]] auto async_wait_handle() noexcept -> xtd::threading::wait_handle& override {return data_->async_event;}
418 [[nodiscard]] auto completed_synchronously() const noexcept -> bool override {return false;}
419
420 template<class item_t, class ...items_t>
421 static auto fill_task_pointers(std::vector<itask*>& itask_pointer, item_t& first, items_t& ... rest) -> void {
422 itask_pointer.push_back(&first);
423 fill_task_pointers(itask_pointer, rest...);
424 }
425 template<class item_t>
426 static auto fill_task_pointers(std::vector<itask*>& itask_pointer, item_t& item) -> void {
427 itask_pointer.push_back(&item);
428 }
429
430 static auto generate_id() noexcept -> xtd::size {
431 static auto id = xtd::size {0};
432 return ++id;
433 }
434
435 struct data {
436 using result_type = std::conditional_t<std::is_same_v<result_t, void>, std::uint8_t, result_t>;
437
439 xtd::threading::manual_reset_event async_event;
440 xtd::any_object async_state;
442 xtd::action<> continuation;
444 const xtd::any_object empty_state;
445 xtd::threading::auto_reset_event end_event;
446 xtd::exception_services::exception_dispatch_info exception;
448 xtd::size id = generate_id();
450 result_type result;
451 const xtd::any_object* state = &empty_state;
452 xtd::threading::auto_reset_event start_event;
454 xtd::object sync_root;
455
456 xtd::threading::wait_or_timer_callback task_proc {delegate_(const xtd::any_object& state, bool timed_out) {
457 auto previous_current_id = current_id_;
458 current_id_ = id;
459
461 current_id_ = previous_current_id;
462 end_event.set();
463 if (!continuation.is_empty()) continuation();
464 };
465
467 try {
468 if (cancellation_token && cancellation_token->wait_handle().wait_one(0)) xtd::helpers::throw_helper::throws(xtd::helpers::exception_case::task_canceled);
469 if constexpr(std::is_same_v<result_t, void>) {
470 if (!func.is_empty()) func();
471 else if (!parameterized_func.is_empty()) parameterized_func(state);
472 } else {
473 if (!func.is_empty()) result = func();
474 else if (!parameterized_func.is_empty()) result = parameterized_func(state);
475 }
476 if (cancellation_token && cancellation_token->wait_handle().wait_one(0)) xtd::helpers::throw_helper::throws(xtd::helpers::exception_case::task_canceled);
478 } catch (const xtd::threading::tasks::task_canceled_exception& e) {
481 } catch (const xtd::exception& e) {
484 } catch (const std::exception& e) {
485 exception = xtd::exception_services::exception_dispatch_info::capture(xtd::exception {e.what()});
487 } catch (...) {
488 exception = xtd::exception_services::exception_dispatch_info::capture(xtd::exception {"Unknown exception"});
490 }
491 }};
492 };
493
495 inline static thread_local xtd::size current_id_ = 0;
496 };
497 }
498 }
499}
Contains xtd::action delegate.
Contains xtd::aggregate_exception exception.
Contains xtd::threading::auto_reset_event exception.
static constexpr size_t max_value
Definition box_integer.hpp:69
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
static auto throws(xtd::helpers::exception_case exception_case, const source_location &location=source_location::current()) -> void
Throws an exption with specified exception case.
bool set()
Sets the state of the event to signaled, allowing one or more waiting threads to proceed.
Represents an interface for an asynchronous operation.
Definition itask.hpp:19
Provides support for creating and scheduling Task objects.
Definition task_factory.hpp:19
Represents an asynchronous operation.
Definition task_result.hpp:20
static registered_wait_handle 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)
Registers a delegate to wait for a xtd::threading::wait_handle, specifying a 32-bit signed integer fo...
static void sleep(int32 milliseconds_timeout)
Suspends the current thread for a specified time.
static constexpr int32 infinite
A constant used to specify an infinite waiting period. This field is constant.
Definition timeout.hpp:41
static size_t wait_any(const collection_t &wait_handles)
Waits for any of the elements in the specified collection to receive a signal.
Definition wait_handle.hpp:198
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:932
#define lock_(object)
The lock_ keyword marks a statement block as a critical section by obtaining the mutual-exclusion loc...
Definition lock.hpp:67
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
bool yield() noexcept
Suggests that the implementation reschedule execution of threads.
size_t size
Represents a size of any object in bytes.
Definition size.hpp:23
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:23
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
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
delegate< void(arguments_t...)> action
Represents a xtd::delegate that has variable parameters and does not return a value.
Definition action.hpp:20
sptr< type_t > new_sptr(args_t &&... args)
xtd::new_sptr operator creates a xtd::sptr object.
Definition new_sptr.hpp:24
@ start
Starting of a logical operation.
Definition trace_event_type.hpp:37
@ e
The E key.
Definition console_key.hpp:96
Contains xtd::iasync_result interface.
Contains xtd::threading::tasks::itask interface.
virtual auto sync_root() const noexcept -> const xtd::object &=0
Gets an object that can be used to synchronize access to the the xtd::collections::generic::icollecti...
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
read_only_span< type_t, count > first() const
Obtains a subspan consisting of the first count elements of the sequence.
Definition read_only_span.hpp:282
constexpr const_pointer data() const noexcept
Gets direct access to the underlying contiguous storage.
Definition read_only_span.hpp:201
Contains xtd::new_sptr method.
Contains xtd::optional type.
Contains xtd::ref type.
Contains scope_exit_ keyword.
Contains xtd::sptr type.
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_status enum class.
Contains xtd::threading::thread_pool class.
Contains xtd::threading::lock class.
Contains xtd::helpers::throw_helper class.