NexusForce 1.0.0
A Modern C++ Library with extended functionality, web components, and utility libraries
载入中...
搜索中...
未找到
future.hpp
浏览该文件的文档.
1#ifndef NEFORCE_CORE_ASYNC_FUTURE_HPP__
2#define NEFORCE_CORE_ASYNC_FUTURE_HPP__
3
10
21NEFORCE_BEGIN_NAMESPACE__
22
28
33struct future_exception final : exception {
34 explicit future_exception(const char* info = "Future Operation Failed.", const char* type = static_type,
35 const int code = 0) noexcept :
36 exception(info, type, code) {}
37
38 explicit future_exception(const exception& e) :
39 exception(e) {}
40
41 ~future_exception() override = default;
42 static constexpr auto static_type = "future_exception";
43};
44
45 // Exceptions
47
53
59
60template <typename Res>
61class future;
62
63template <typename Res>
64class shared_future;
65
66template <typename Sign>
67class packaged_task;
68
69template <typename Res>
70class promise;
71
72
85
97
98
105enum class launch {
106 async = 1,
108};
109
110constexpr launch operator&(const launch left, const launch right) noexcept {
111 return static_cast<launch>(static_cast<int>(left) & static_cast<int>(right));
112}
113constexpr launch operator|(const launch left, const launch right) noexcept {
114 return static_cast<launch>(static_cast<int>(left) | static_cast<int>(right));
115}
116constexpr launch operator^(const launch left, const launch right) noexcept {
117 return static_cast<launch>(static_cast<int>(left) ^ static_cast<int>(right));
118}
119constexpr launch operator~(const launch value) noexcept { return static_cast<launch>(~static_cast<int>(value)); }
120
121constexpr launch& operator&=(launch& left, const launch right) noexcept { return left = left & right; }
122constexpr launch& operator|=(launch& left, const launch right) noexcept { return left = left | right; }
123constexpr launch& operator^=(launch& left, const launch right) noexcept { return left = left ^ right; }
124
125
132template <typename Func, typename... Args>
134
135template <typename Func, typename... Args>
136future<async_result_t<Func, Args...>> async(launch policy, Func&& function, Args&&... args);
137
138template <typename Func, typename... Args>
139future<async_result_t<Func, Args...>> async(Func&& function, Args&&... args);
140
141
143NEFORCE_BEGIN_INNER__
144
145constexpr const char* future_errc_cstr(const future_errc code) {
146 switch (code) {
148 return "future_already_retrieved";
149 }
151 return "promise_already_satisfied";
152 }
154 return "no_state";
155 }
157 return "broken_promise";
158 }
159 default: {
160 unreachable();
161 }
162 }
163}
164
171struct __future_base {
178 struct result_base {
179 exception_ptr error_ptr{nullptr};
180
181 protected:
182 result_base() noexcept = default;
183
184 public:
185 result_base(const result_base&) = delete;
186 result_base& operator=(const result_base&) = delete;
187
188 virtual ~result_base() = default;
189
194 virtual void destroy() = 0;
195
200 struct Deleter {
201 void operator()(result_base* result) const { result->destroy(); }
202 };
203 };
204
205 template <typename Res>
206 using Ptr = unique_ptr<Res, result_base::Deleter>;
207
215 template <typename Res>
216 struct basic_result : result_base {
217 private:
218 aligned_buffer<Res> storage;
219 bool initialized = false;
220
222 void destroy() override { delete this; }
223
224 public:
225 using result_type = Res;
226
230 basic_result() noexcept = default;
231
236 ~basic_result() override {
237 if (initialized) {
238 value().~Res();
239 }
240 }
241
246 Res& value() noexcept { return *storage.ptr(); }
247
252 void set(Res&& result) {
253 new (storage.addr()) Res(_NEFORCE forward<Res>(result));
254 initialized = true;
255 }
256 };
257
266 template <typename Res, typename Alloc>
267 struct allocated_result final : basic_result<Res>, Alloc {
268 using allocator_type = inner::__allocator_traits_base::alloc_rebind_t<Alloc, allocated_result>;
269
274 explicit allocated_result(const Alloc& alloc) :
275 basic_result<Res>(),
276 Alloc(alloc) {}
277
278 private:
282 void destroy() override {
283 allocator_type alloc(*this);
284 allocated_ptr<allocator_type> guard_ptr{alloc, this};
285 this->~AllocatedResult();
286 }
287 };
288
296 template <typename Res, typename Alloc>
297 static Ptr<allocated_result<Res, Alloc>> allocate_result(const Alloc& alloc) {
298 using result_type = allocated_result<Res, Alloc>;
299 typename result_type::allocator_type alloc2(alloc);
300 auto guard = _NEFORCE allocate_guarded(alloc2);
301 auto* ptr = new (static_cast<void*>(guard.get())) result_type{alloc};
302 guard = nullptr;
303 return Ptr<result_type>(ptr);
304 }
305
312 template <typename Res, typename T>
313 static Ptr<basic_result<Res>> allocate_result(const _NEFORCE allocator<T>& /*unused*/) {
314 return Ptr<basic_result<Res>>(new basic_result<Res>);
315 }
316
323 class state_base {
324 public:
325 using PtrType = Ptr<result_base>;
326
327 private:
329 enum status : uint32_t {
330 not_ready,
331 ready
332 };
333
334 PtrType result_ptr;
335 atomic_futex<> status{status::not_ready};
336 atomic_flag retrieved;
337 once_flag flag;
338
339 public:
343 state_base() noexcept { retrieved.clear(); }
344
345 state_base(const state_base&) = delete;
346 state_base& operator=(const state_base&) = delete;
347 virtual ~state_base() = default;
348
354 result_base& wait() {
355 complete_async();
356 status.load_when_equal(status::ready, memory_order_acquire);
357 return *result_ptr;
358 }
359
367 template <typename Rep, typename Period>
368 future_status wait_for(const duration<Rep, Period>& relative_time) {
369 if (status.load(memory_order_acquire) == status::ready) {
371 }
372
373 if (is_deferred_future()) {
375 }
376
379 complete_async();
381 }
383 }
384
392 template <typename Clock, typename Dur>
393 future_status wait_until(const time_point<Clock, Dur>& absolute_time) {
394 static_assert(is_clock_v<Clock>, "Clock type must be clock_t");
395 if (status.load(memory_order_acquire) == status::ready) {
397 }
398
399 if (is_deferred_future()) {
401 }
402
403 if (status.load_when_equal_until(status::ready, memory_order_acquire, absolute_time)) {
404 complete_async();
406 }
408 }
409
417 template <typename Callable>
418 void set_result(Callable&& result_func, const bool ignore_failure = false) {
419 bool did_set = false;
420 function<PtrType()> func = _NEFORCE forward<Callable>(result_func);
421 call_once(flag, &state_base::do_set, this, _NEFORCE addressof(func), _NEFORCE addressof(did_set));
422 if (did_set) {
423 status.store_notify_all(status::ready, memory_order_release);
424 } else if (!ignore_failure) {
425 NEFORCE_THROW_EXCEPTION(future_exception(future_errc_cstr(future_errc::promise_already_satisfied)));
426 }
427 }
428
437 template <typename Callable>
438 void set_delayed_result(Callable&& result_func, weak_ptr<state_base> self) {
439 bool did_set = false;
440 unique_ptr<make_ready> mr = make_unique<make_ready>();
441 function<PtrType()> func = _NEFORCE forward<Callable>(result_func);
442 call_once(flag, &state_base::do_set, this, _NEFORCE addressof(func), _NEFORCE addressof(did_set));
443 if (!did_set) {
444 NEFORCE_THROW_EXCEPTION(future_exception(future_errc_cstr(future_errc::promise_already_satisfied)));
445 }
446 mr->shared_state = _NEFORCE move(self);
447 mr->set();
448 mr.release();
449 }
450
456 void break_promise(PtrType result) {
457 if (static_cast<bool>(result)) {
458 result->error_ptr = make_exception_ptr(future_exception(future_errc_cstr(future_errc::broken_promise)));
459 result_ptr.swap(result);
460 status.store_notify_all(status::ready, memory_order_release);
461 }
462 }
463
468 void set_retrieved_flag() {
469 if (retrieved.test_and_set(memory_order_acquire)) {
470 NEFORCE_THROW_EXCEPTION(future_exception(future_errc_cstr(future_errc::future_already_retrieved)));
471 }
472 }
473
481 template <typename Res, typename Arg>
482 struct setter;
483
487 template <typename Res, typename Arg>
488 struct setter<Res, Arg&> {
489 static_assert(is_same_v<Res, Arg&> || is_same_v<const Res, Arg>, "Invalid specialisation");
490
491 typename promise<Res>::ptr_type operator()() const {
492 promise_ptr->storage->set(*arg_ptr);
493 return _NEFORCE move(promise_ptr->storage);
494 }
495 promise<Res>* promise_ptr;
496 Arg* arg_ptr;
497 };
498
502 template <typename Res>
503 struct setter<Res, Res&&> {
504 typename promise<Res>::ptr_type operator()() const {
505 promise_ptr->storage->set(_NEFORCE move(*arg_ptr));
506 return _NEFORCE move(promise_ptr->storage);
507 }
508 promise<Res>* promise_ptr;
509 Res* arg_ptr;
510 };
511
515 template <typename Res>
516 struct setter<Res, void> {
517 static_assert(is_void_v<Res>, "Only used for promise<void>");
518
519 typename promise<Res>::ptr_type operator()() const { return _NEFORCE move(promise_ptr->storage); }
520 promise<Res>* promise_ptr;
521 };
522
523 struct exception_ptr_tag {};
524
528 template <typename Res>
529 struct setter<Res, exception_ptr_tag> {
530 typename promise<Res>::ptr_type operator()() const {
531 promise_ptr->storage->error_ptr = *exp_ptr;
532 return _NEFORCE move(promise_ptr->storage);
533 }
534 promise<Res>* promise_ptr;
535 exception_ptr* exp_ptr;
536 };
537
541 template <typename Res, typename Arg>
542 NEFORCE_ALWAYS_INLINE static setter<Res, Arg&&> create_setter(promise<Res>* promise_ptr, Arg&& arg) noexcept {
543 return setter<Res, Arg&&>{promise_ptr, _NEFORCE addressof(arg)};
544 }
545
549 template <typename Res>
550 NEFORCE_ALWAYS_INLINE static setter<Res, exception_ptr_tag> create_setter(exception_ptr& exception,
551 promise<Res>* promise_ptr) noexcept {
552 return setter<Res, exception_ptr_tag>{promise_ptr, &exception};
553 }
554
558 template <typename Res>
559 NEFORCE_ALWAYS_INLINE static setter<Res, void> create_setter(promise<Res>* promise_ptr) noexcept {
560 return setter<Res, void>{promise_ptr};
561 }
562
569 template <typename T>
570 static void check(const shared_ptr<T>& ptr) {
571 if (!static_cast<bool>(ptr)) {
572 NEFORCE_THROW_EXCEPTION(future_exception(future_errc_cstr(future_errc::no_state)));
573 }
574 }
575
576 private:
582 void do_set(function<PtrType()>* func, bool* did_set) {
583 PtrType result = (*func)();
584 *did_set = true;
585 result_ptr.swap(result);
586 }
587
592 virtual void complete_async() {}
593
598 NEFORCE_NODISCARD virtual bool is_deferred_future() const { return false; }
599
605 struct make_ready final : at_thread_exit_elt {
606 weak_ptr<state_base> shared_state;
607
612 static void run(void* ptr) noexcept {
613 auto* const self = static_cast<make_ready*>(ptr);
614 const auto state = self->shared_state.lock();
615 if (state) {
616 state->status.store_notify_all(status::ready, memory_order_release);
617 }
618 delete self;
619 }
620
624 void set() { thread_exit_register(this, &make_ready::run); }
625 };
626 };
627
628private:
633 template <typename Ptr>
634 struct get_result_type {
635 using type = typename Ptr::element_type::result_type;
636 };
637
638 template <typename Ptr>
639 using result_res_t = typename get_result_type<Ptr>::type;
640
641public:
642 class async_state_common;
643
644 template <typename BoundFunc, typename Res = decltype(_NEFORCE declval<BoundFunc&>()())>
645 class deferred_state;
646
647 template <typename BoundFunc, typename Res = decltype(_NEFORCE declval<BoundFunc&>()())>
648 class async_state_impl;
649
650 template <typename Sign>
651 class task_state_base;
652
653 template <typename Func, typename Alloc, typename Sign>
654 class task_state;
655
656 template <typename ResPtrT, typename Func, typename ResT = result_res_t<ResPtrT>>
657 struct task_setter;
658
662 template <typename ResPtr, typename BoundFunc>
663 static task_setter<ResPtr, BoundFunc> create_task_setter(ResPtr& ptr, BoundFunc& call) {
664 return {_NEFORCE addressof(ptr), _NEFORCE addressof(call)};
665 }
666};
667
672template <typename Res>
673struct __future_base::basic_result<Res&> : __future_base::result_base {
674private:
675 Res* value_ptr;
676
677 void destroy() override { delete this; }
678
679public:
680 using result_type = Res&;
681
685 basic_result() noexcept :
686 value_ptr() {}
687
692 void set(Res& result) noexcept { value_ptr = _NEFORCE addressof(result); }
693
698 NEFORCE_NODISCARD Res& get() noexcept { return *value_ptr; }
699};
700
704template <>
705struct __future_base::basic_result<void> : __future_base::result_base {
706 using result_type = void;
707
708private:
709 void destroy() override { delete this; }
710};
711
712
719template <typename Res>
720class __basic_future : public __future_base {
721protected:
722 using state_type = shared_ptr<state_base>;
723 using result_type = __future_base::basic_result<Res>&;
724
725private:
726 state_type state_ptr;
727
728public:
729 __basic_future(const __basic_future&) = delete;
730 __basic_future& operator=(const __basic_future&) = delete;
731
736 NEFORCE_NODISCARD bool valid() const noexcept { return static_cast<bool>(state_ptr); }
737
742 void wait() const {
743 state_base::check(state_ptr);
744 state_ptr->wait();
745 }
746
755 template <typename Rep, typename Period>
756 future_status wait_for(const duration<Rep, Period>& relative_time) const {
757 state_base::check(state_ptr);
758 return state_ptr->wait_for(relative_time);
759 }
760
769 template <typename Clock, typename Dur>
770 future_status wait_until(const time_point<Clock, Dur>& absolute_time) const {
771 state_base::check(state_ptr);
772 return state_ptr->wait_until(absolute_time);
773 }
774
775protected:
780 NEFORCE_NODISCARD result_type get_result() const {
781 state_base::check(state_ptr);
782 result_base& result = state_ptr->wait();
783 if (result.error_ptr != nullptr) {
784 rethrow_exception(result.error_ptr);
785 }
786 return static_cast<result_type>(result);
787 }
788
793 void swap(__basic_future& other) noexcept { state_ptr.swap(other.state_ptr); }
794
799 explicit __basic_future(state_type state) :
800 state_ptr(move(state)) {
801 state_base::check(state_ptr);
802 state_ptr->set_retrieved_flag();
803 }
804
805 explicit __basic_future(const shared_future<Res>& other) noexcept;
806 explicit __basic_future(shared_future<Res>&& other) noexcept;
807 explicit __basic_future(future<Res>&& other) noexcept;
808
812 constexpr __basic_future() noexcept = default;
813
819 struct reset {
820 __basic_future& future_ref;
821
826 explicit reset(__basic_future& future) noexcept :
827 future_ref(future) {}
828
832 ~reset() { future_ref.state_ptr.reset(); }
833 };
834};
835
836NEFORCE_END_INNER__
838
839
840template <typename Res, typename Arg>
841struct is_location_invariant<inner::__future_base::state_base::setter<Res, Arg>> : true_type {};
842
843template <typename ResPtr, typename Func, typename Res>
844struct is_location_invariant<inner::__future_base::task_setter<ResPtr, Func, Res>> : true_type {};
845
846
855template <typename Res>
856class future : public inner::__basic_future<Res> {
857 static_assert(!is_array_v<Res>, "result type must not be an array");
858 static_assert(!is_function_v<Res>, "result type must not be a function");
859 static_assert(is_destructible_v<Res>, "result type must be destructible");
860
861 friend class promise<Res>;
862
863 template <typename Sign>
864 friend class packaged_task;
865
866 template <typename Function, typename... Args>
867 friend future<async_result_t<Function, Args...>> async(launch, Function&&, Args&&...);
868
869 using base_type = inner::__basic_future<Res>;
870 using state_type = typename base_type::state_type;
871
875 explicit future(const state_type& state) :
876 base_type(state) {}
877
878public:
882 constexpr future() noexcept :
883 base_type() {}
884
889 future(future&& other) noexcept :
890 base_type(_NEFORCE move(other)) {}
891
892 future(const future&) = delete;
893 future& operator=(const future&) = delete;
894
900 future& operator=(future&& other) noexcept {
901 future(_NEFORCE move(other)).swap(*this);
902 return *this;
903 }
904
910 Res get() {
911 typename base_type::reset reset_(*this);
912 return _NEFORCE move(this->get_result().value());
913 }
914
920};
921
926template <typename Res>
927class future<Res&> : public inner::__basic_future<Res&> {
928 friend class promise<Res&>;
929
930 template <typename Sign>
931 friend class packaged_task;
932
933 template <typename Function, typename... Args>
934 friend future<async_result_t<Function, Args...>> async(launch, Function&&, Args&&...);
935
936 using base_type = inner::__basic_future<Res&>;
937 using state_type = typename base_type::state_type;
938
939 explicit future(const state_type& state) :
940 base_type(state) {}
941
942public:
943 constexpr future() noexcept :
944 base_type() {}
945 future(future&& other) noexcept :
946 base_type(_NEFORCE move(other)) {}
947
948 future(const future&) = delete;
949 future& operator=(const future&) = delete;
950
951 future& operator=(future&& other) noexcept {
952 future(_NEFORCE move(other)).swap(*this);
953 return *this;
954 }
955
961 Res& get() {
962 typename base_type::reset reset_(*this);
963 return this->get_result().get();
964 }
965
966 shared_future<Res&> share() noexcept;
967};
968
972template <>
973class future<void> : public inner::__basic_future<void> {
974 friend class promise<void>;
975
976 template <typename>
977 friend class packaged_task;
978
979 template <typename Function, typename... Args>
980 friend future<async_result_t<Function, Args...>> async(launch, Function&&, Args&&...);
981
982 using base_type = inner::__basic_future<void>;
983 // NOLINTNEXTLINE(readability-redundant-qualified-alias)
984 using state_type = base_type::state_type;
985
986 explicit future(state_type state) :
987 base_type(move(state)) {}
988
989public:
990 future() noexcept = default;
991
992 future(future&& other) noexcept :
993 base_type(_NEFORCE move(other)) {}
994
995 future(const future&) = delete;
996 future& operator=(const future&) = delete;
997
998 future& operator=(future&& other) noexcept {
999 future(_NEFORCE move(other)).swap(*this);
1000 return *this;
1001 }
1002
1007 void get() {
1008 base_type::reset reset(*this);
1009 ignore = this->get_result();
1010 }
1011
1012 shared_future<void> share() noexcept;
1013};
1014
1022template <typename Res>
1023class shared_future : public inner::__basic_future<Res> {
1024 static_assert(!is_array_v<Res>, "result type must not be an array");
1025 static_assert(!is_function_v<Res>, "result type must not be a function");
1026 static_assert(is_destructible_v<Res>, "result type must be destructible");
1027
1028 using base_type = inner::__basic_future<Res>;
1029
1030public:
1031 constexpr shared_future() noexcept :
1032 base_type() {}
1033 shared_future(const shared_future& other) noexcept :
1034 base_type(other) {}
1035 shared_future(future<Res>&& other) noexcept :
1036 base_type(_NEFORCE move(other)) {}
1037 shared_future(shared_future&& other) noexcept :
1038 base_type(_NEFORCE move(other)) {}
1039
1040 shared_future& operator=(const shared_future& other) noexcept {
1041 shared_future(other).swap(*this);
1042 return *this;
1043 }
1044
1045 shared_future& operator=(shared_future&& other) noexcept {
1046 shared_future(_NEFORCE move(other)).swap(*this);
1047 return *this;
1048 }
1049
1055 const Res& get() const { return this->get_result().value(); }
1056};
1057
1062template <typename Res>
1063class shared_future<Res&> : public inner::__basic_future<Res&> {
1064 using base_type = inner::__basic_future<Res&>;
1065
1066public:
1067 constexpr shared_future() noexcept :
1068 base_type() {}
1069 shared_future(const shared_future& other) :
1070 base_type(other) {}
1071 shared_future(future<Res&>&& other) noexcept :
1072 base_type(_NEFORCE move(other)) {}
1073 shared_future(shared_future&& other) noexcept :
1074 base_type(_NEFORCE move(other)) {}
1075
1076 shared_future& operator=(const shared_future& other) {
1077 shared_future(other).swap(*this);
1078 return *this;
1079 }
1080
1081 shared_future& operator=(shared_future&& other) noexcept {
1082 shared_future(_NEFORCE move(other)).swap(*this);
1083 return *this;
1084 }
1085
1090 Res& get() const { return this->get_result().get(); }
1091};
1092
1096template <>
1097class shared_future<void> : public inner::__basic_future<void> {
1098 using base_type = inner::__basic_future<void>;
1099
1100public:
1101 shared_future() noexcept = default;
1102
1103 shared_future(const shared_future& other) :
1104 base_type(other) {}
1105 shared_future(future<void>&& other) noexcept :
1106 base_type(_NEFORCE move(other)) {}
1107 shared_future(shared_future&& other) noexcept :
1108 base_type(_NEFORCE move(other)) {}
1109
1110 shared_future& operator=(const shared_future& other) {
1111 shared_future(other).swap(*this);
1112 return *this;
1113 }
1114
1115 shared_future& operator=(shared_future&& other) noexcept {
1116 shared_future(_NEFORCE move(other)).swap(*this);
1117 return *this;
1118 }
1119
1123 void get() const { ignore = this->get_result(); }
1124};
1125
1127
1128template <typename Res>
1129inner::__basic_future<Res>::__basic_future(const shared_future<Res>& other) noexcept :
1130state_ptr(other.state_ptr) {}
1131
1132template <typename Res>
1133inner::__basic_future<Res>::__basic_future(shared_future<Res>&& other) noexcept :
1134state_ptr(_NEFORCE move(other.state_ptr)) {}
1135
1136template <typename Res>
1137inner::__basic_future<Res>::__basic_future(future<Res>&& other) noexcept :
1138state_ptr(_NEFORCE move(other.state_ptr)) {}
1139
1140
1141template <typename Result>
1142shared_future<Result> future<Result>::share() noexcept {
1143 return shared_future<Result>(_NEFORCE move(*this));
1144}
1145
1146template <typename Res>
1147shared_future<Res&> future<Res&>::share() noexcept {
1148 return shared_future<Res&>(_NEFORCE move(*this));
1149}
1150
1151inline shared_future<void> future<void>::share() noexcept { return {_NEFORCE move(*this)}; }
1152
1154
1161template <typename T>
1163 using type = T;
1164};
1165
1169template <>
1170struct future_result<void> {
1171 using type = none_t;
1172};
1173
1178template <typename T>
1179using future_result_t = typename future_result<T>::type;
1180
1181
1188template <typename T>
1193
1200template <typename T>
1204 // Async
1206 // AsyncComponents
1208
1209NEFORCE_END_NAMESPACE__
1210#endif // NEFORCE_CORE_ASYNC_FUTURE_HPP__
对齐缓冲区实现
分配器指针包装器
线程退出回调支持
原子快速用户态互斥锁实现
单次调用
uint32_t load(const memory_order mo) const noexcept
原子加载数据
void load_when_equal(const uint32_t value, const memory_order mo)
等待直到值等于指定值
void store_notify_all(const uint32_t value, const memory_order mo) noexcept
存储新值并通知所有等待线程
bool load_when_equal_for(const uint32_t value, const memory_order mo, const duration< Rep, Period > &rtime)
在指定时间内等待值等于指定值
bool load_when_equal_until(const uint32_t value, const memory_order mo, const time_point< Clock, Dur > &atime)
在指定时间点前等待值等于指定值(通用时钟)
函数包装器主模板声明
Res & get()
获取结果引用
void get()
获取结果
独占future类模板
Res get()
获取结果
future & operator=(future &&other) noexcept
移动赋值运算符
constexpr future() noexcept
默认构造函数
future(future &&other) noexcept
移动构造函数
shared_future< Res > share() noexcept
转换为共享future
Promise类模板
inner::__future_base::Ptr< result_type > ptr_type
结果指针类型
集合容器
Res & get() const
获取结果引用
void get() const
获取结果
共享future类模板
const Res & get() const
获取常量引用结果
constexpr pointer release() noexcept
释放所有权
shared_ptr< T > lock() const noexcept
尝试获取共享智能指针
异常指针实现
通用函数包装器
allocated_ptr< Alloc > allocate_guarded(Alloc &alloc)
分配内存并创建allocated_ptr
constexpr T && forward(remove_reference_t< T > &x) noexcept
完美转发左值
constexpr T * addressof(T &x) noexcept
获取对象的地址
future_errc
期值错误码枚举
future< async_result_t< Func, Args... > > async(launch policy, Func &&function, Args &&... args)
异步执行函数(指定策略)
invoke_result_t< decay_t< Func >, decay_t< Args >... > async_result_t
异步调用结果类型推导
launch
异步启动策略枚举
enable_if_t< is_void_v< T >, future_result_t< T > > get(future< T > &f)
通用future结果获取函数
future_status
期值状态枚举
typename future_result< T >::type future_result_t
future结果类型别名
@ broken_promise
承诺被破坏
@ future_already_retrieved
future已经被获取
@ promise_already_satisfied
promise已经被满足
@ no_state
没有关联的状态对象
@ async
异步执行,在新线程中运行
@ deferred
延迟执行
@ timeout
等待超时
@ ready
结果已就绪
constexpr bool is_void_v
is_void的便捷变量模板
constexpr bool is_array_v
is_array的便捷变量模板
constexpr bool is_function_v
is_function的便捷变量模板
void call_once(once_flag &flag, Callable &&func, Args &&... args)
单次调用函数
constexpr bool is_clock_v
is_clock的便捷变量模板
milliseconds relative_time(int64_t sec, int64_t nsec, bool is_monotonic=false) noexcept
将绝对时间戳转换为相对延迟毫秒数
unsigned int uint32_t
32位无符号整数类型
NEFORCE_NORETURN void unreachable() noexcept
标记不可达代码路径
NEFORCE_NORETURN void rethrow_exception(const exception_ptr &p)
重新抛出异常
exception_ptr make_exception_ptr(Ex ex) noexcept
创建异常指针
@ wait
等待操作
constexpr void destroy(T *pointer) noexcept(is_nothrow_destructible_v< T >)
销毁单个对象
typename inner::__invoke_result_aux< F, Args... >::type invoke_result_t
invoke_result的便捷别名
standard_allocator< T > allocator
标准分配器别名
constexpr auto memory_order_release
释放内存顺序常量
constexpr auto memory_order_acquire
获取内存顺序常量
constexpr none_t none
默认空表示
constexpr Iterator2 move(Iterator1 first, Iterator1 last, Iterator2 result) noexcept(noexcept(inner::__move_aux(first, last, result)))
移动范围元素
void swap()=delete
删除无参数的swap重载
void thread_exit_register(at_thread_exit_elt *elt, void(*callback)(void *)) noexcept
注册线程退出回调
typename decay< T >::type decay_t
decay的便捷别名
constexpr bool is_destructible_v
is_destructible的便捷变量模板
bool_constant< true > true_type
表示true的类型
constexpr bool is_same_v
is_same的便捷变量模板
typename enable_if< Test, T >::type enable_if_t
enable_if的便捷别名
constexpr unique_ptr< T > make_unique(Args &&... args)
创建unique_ptr
空状态类型
标准分配器
void * addr() noexcept
获取缓冲区的原始地址
T * ptr() noexcept
获取缓冲区的类型化指针
bool test_and_set(const memory_order mo=memory_order_seq_cst) noexcept
测试并设置标志
void clear(const memory_order mo=memory_order_seq_cst) noexcept
清除标志
static constexpr duration zero() noexcept
获取零持续时间
exception(const char *info=static_type, const char *type=static_type, const int code=0)
构造函数
const char * type() const noexcept
获取异常类型
int code() const noexcept
获取异常码
future结果类型转换
判断类型是否是位置不变的
空状态类型
弱智能指针实现