68 template <
class F,
class... Args>
69 explicit Task(F &&f, Args &&...args) {
70 start(std::forward<F>(f), std::forward<Args>(args)...);
83 future_(std::move(other.future_)),
84 thread_(std::move(other.thread_)),
85 event_(std::move(other.event_)) {}
96 future_ = std::move(other.future_);
97 thread_ = std::move(other.thread_);
98 event_ = std::move(other.event_);
119 template <
class F,
class... Args>
120 typename std::enable_if<
124 = std::bind(std::forward<F>(f), std::forward<Args>(args)...);
125 std::packaged_task<
T()> task(std::move(bound_task));
127 thread_ = std::thread(std::move(task));
130 template <
class F,
class... Args>
131 typename std::enable_if<
135 start(std::forward<F>(f),
137 std::forward<Args>(args)...);
162 template <
class Rep,
class Period>
164 wait_for(
const std::chrono::duration<Rep, Period> &timeout)
const {
165 return future_.wait_for(timeout);
174 return future_.wait_for(std::chrono::nanoseconds(timeout.
count()));
191 template <
class Clock,
class Duration>
193 const std::chrono::time_point<Clock, Duration> &time_point)
const {
194 return future_.wait_until(time_point);
202 return future_.wait_for(std::chrono::nanoseconds(0));
210 if (
status() == std::future_status::ready)
return;
212 std::promise<T> promise;
213 future_ = promise.get_future();
214 promise.set_exception(std::make_exception_ptr(
234 std::shared_ptr<std::once_flag>
flag_;
241 if (t && t->joinable()) t->join();
264 class =
typename std::enable_if<
268 flag_(new std::once_flag()) {
269 start(std::forward<F>(f), std::forward<Args>(args)...);
300 future_(std::move(other.future_)),
301 thread_(std::move(other.thread_)),
302 event_(std::move(other.event_)),
303 flag_(std::move(other.flag_)) {}
311 if (
this != &other) {
312 future_ = std::move(other.future_);
313 thread_ = std::move(other.thread_);
314 event_ = std::move(other.event_);
315 flag_ = std::move(other.flag_);
333 template <
class F,
class... Args>
334 typename std::enable_if<
338 = std::bind(std::forward<F>(f), std::forward<Args>(args)...);
339 std::packaged_task<
T()> task(std::move(bound_task));
340 future_ = task.get_future().share();
341 thread_ = std::shared_ptr<std::thread>(
345 template <
class F,
class... Args>
346 typename std::enable_if<
351 start(std::forward<F>(f),
353 std::forward<Args>(args)...);
378 template <
class Rep,
class Period>
380 wait_for(
const std::chrono::duration<Rep, Period> &timeout)
const {
381 return future_.wait_for(timeout);
390 return future_.wait_for(std::chrono::nanoseconds(timeout.
count()));
407 template <
class Clock,
class Duration>
409 const std::chrono::time_point<Clock, Duration> &time_point)
const {
410 return future_.wait_until(time_point);
418 return future_.wait_for(std::chrono::nanoseconds(0));
427 std::call_once(*
flag_, [
this] {
428 if (
status() == std::future_status::ready)
return;
430 std::promise<T> promise;
431 future_ = promise.get_future().share();
432 promise.set_exception(std::make_exception_ptr(
433 AsyncError(
"SharedTask has been cancelled.")));
AsyncError(const std::string &msg)
构造 ConsoleError。
Definition csexc.h:193
线程同步事件类,用于线程间通信。
Definition event.h:42
Event()
默认构造函数,初始化事件标志为 false。
Definition event.h:49
void set()
设置事件标志为 true,并通知所有等待的线程。
Definition event.h:52
std::shared_ptr< Event > event_
事件标志
Definition task.h:233
std::future_status wait_for(double seconds) const
等待异步任务完成,最多等待指定时间。
Definition task.h:398
SharedTask & operator=(const SharedTask &other)
拷贝赋值运算符。
Definition task.h:287
std::enable_if< is_callable< F, constEvent &, Args... >::value >::type start(F &&f, Args &&...args)
Definition task.h:348
std::future_status wait_for(const std::chrono::duration< Rep, Period > &timeout) const
等待异步任务完成,最多等待指定时间。
Definition task.h:380
SharedTask(F &&f, Args &&...args)
构造函数,接受一个可调用对象和参数,启动异步任务。
Definition task.h:267
static void event_deleter(Event *e)
事件删除器,用于在 std::shared_ptr 销毁时自动调用。
Definition task.h:248
SharedTask & operator=(SharedTask &&other) noexcept
移动赋值运算符。
Definition task.h:310
T get()
获取异步任务的返回值。
Definition task.h:360
std::shared_ptr< std::thread > thread_
任务执行的线程
Definition task.h:232
SharedTask(SharedTask &&other) noexcept
移动构造函数。
Definition task.h:299
std::shared_future< T > future_
任务的返回值
Definition task.h:231
SharedTask()
默认构造函数。
Definition task.h:252
std::future_status wait_until(const std::chrono::time_point< Clock, Duration > &time_point) const
等待异步任务完成,最多等待指定时间。
Definition task.h:408
void wait() const
等待异步任务完成。
Definition task.h:369
void cancel()
取消异步任务。
Definition task.h:426
~SharedTask()=default
析构函数。
std::enable_if<!is_callable< F, constEvent &, Args... >::value >::type start(F &&f, Args &&...args)
启动异步任务。
Definition task.h:336
SharedTask(const SharedTask &other)
拷贝构造函数。
Definition task.h:276
std::future_status status() const
获取异步任务的状态。
Definition task.h:417
std::shared_ptr< std::once_flag > flag_
取消标志
Definition task.h:234
std::future_status wait_for(Time timeout) const
等待异步任务完成,最多等待指定时间。
Definition task.h:389
static void thread_deleter(std::thread *t)
线程删除器,用于在 std::shared_ptr 销毁时自动调用。
Definition task.h:240
bool valid()
检查异步任务是否有效。
Definition task.h:366
~Task()
析构函数。
Definition task.h:106
Task & operator=(Task &&other) noexcept
移动赋值运算符。
Definition task.h:92
Task(F &&f, Args &&...args)
构造函数,接受一个可调用对象和参数,启动异步任务。
Definition task.h:69
std::future_status wait_until(const std::chrono::time_point< Clock, Duration > &time_point) const
等待异步任务完成,最多等待指定时间。
Definition task.h:192
void cancel()
取消异步任务。
Definition task.h:209
std::thread thread_
任务执行的线程
Definition task.h:53
std::future_status wait_for(const std::chrono::duration< Rep, Period > &timeout) const
等待异步任务完成,最多等待指定时间。
Definition task.h:164
Task & operator=(const Task &)=delete
删除拷贝赋值运算符。
Task(Task &&other) noexcept
移动构造函数。
Definition task.h:82
std::enable_if< is_callable< F, constEvent &, Args... >::value >::type start(F &&f, Args &&...args)
Definition task.h:133
bool valid()
检查异步任务是否有效。
Definition task.h:150
std::future_status wait_for(Time timeout) const
等待异步任务完成,最多等待指定时间。
Definition task.h:173
T get()
获取异步任务的返回值。
Definition task.h:144
std::enable_if<!is_callable< F, constEvent &, Args... >::value >::type start(F &&f, Args &&...args)
启动异步任务。
Definition task.h:122
std::future_status wait_for(double seconds) const
等待异步任务完成,最多等待指定时间。
Definition task.h:182
std::future_status status() const
获取异步任务的状态。
Definition task.h:201
void wait() const
等待异步任务完成。
Definition task.h:153
std::unique_ptr< Event > event_
事件标志
Definition task.h:54
Task(const Task &)=delete
删除拷贝构造函数。
std::future< T > future_
任务的返回值
Definition task.h:52
表示以纳秒为单位的时间量,支持单位转换、算术运算和自动选择合适的输出单位。
Definition time.h:57
intmax_t count() const
获取纳秒数。
Definition time.h:68
提供线程同步的 Event 类,支持设置、清除、等待和超时等待等操作。
is_callable_impl< F(Args...), void > is_callable
Definition sfinae.h:82
Time seconds(double n)
静态函数,构造秒。
Definition time.h:178
提供编译期类型特征检测(SFINAE 工具), 用于判断容器、可调用对象、迭代器、下标访问、字符串、可打印类型、字符类型等。