Console Library 8.0.0
A header-only library that makes C++ simple
Loading...
Searching...
No Matches
console::SharedTask< T > Class Template Reference

异步任务类,用于在后台线程中执行任务 (共享版)。 More...

#include <task.h>

Public Member Functions

 SharedTask ()
 默认构造函数。
template<class F, class... Args, class = typename std::enable_if< is_callable<F, Args...>::value || is_callable<F, const Event &, Args...>::value>::type>
 SharedTask (F &&f, Args &&...args)
 构造函数,接受一个可调用对象和参数,启动异步任务。
 SharedTask (const SharedTask &other)
 拷贝构造函数。
SharedTask & operator= (const SharedTask &other)
 拷贝赋值运算符。
 SharedTask (SharedTask &&other) noexcept
 移动构造函数。
SharedTask & operator= (SharedTask &&other) noexcept
 移动赋值运算符。
 ~SharedTask ()=default
 析构函数。
template<class F, class... Args>
std::enable_if<!is_callable< F, constEvent &, Args... >::value >::type start (F &&f, Args &&...args)
 启动异步任务。
template<class F, class... Args>
std::enable_if< is_callable< F, constEvent &, Args... >::value >::type start (F &&f, Args &&...args)
T get ()
 获取异步任务的返回值。
bool valid ()
 检查异步任务是否有效。
void wait () const
 等待异步任务完成。
template<class Rep, class Period>
std::future_status wait_for (const std::chrono::duration< Rep, Period > &timeout) const
 等待异步任务完成,最多等待指定时间。
std::future_status wait_for (Time timeout) const
 等待异步任务完成,最多等待指定时间。
std::future_status wait_for (double seconds) const
 等待异步任务完成,最多等待指定时间。
template<class Clock, class Duration>
std::future_status wait_until (const std::chrono::time_point< Clock, Duration > &time_point) const
 等待异步任务完成,最多等待指定时间。
std::future_status status () const
 获取异步任务的状态。
void cancel ()
 取消异步任务。

Static Private Member Functions

static void thread_deleter (std::thread *t)
 线程删除器,用于在 std::shared_ptr 销毁时自动调用。
static void event_deleter (Event *e)
 事件删除器,用于在 std::shared_ptr 销毁时自动调用。

Private Attributes

std::shared_future< T > future_
 任务的返回值
std::shared_ptr< std::thread > thread_
 任务执行的线程
std::shared_ptr< Event > event_
 事件标志
std::shared_ptr< std::once_flag > flag_
 取消标志

Detailed Description

template<class T>
class console::SharedTask< T >

异步任务类,用于在后台线程中执行任务 (共享版)。

Template Parameters
T任务返回值的类型。
Note
SharedTask 的限制相对于 Task 更少,如:
  • 线程安全,无需手动同步。
  • 可多次获取结果,不会抛出异常。
  • 可拷贝,也可移动。 但可能略慢。

Constructor & Destructor Documentation

◆ SharedTask() [1/4]

template<class T>
console::SharedTask< T >::SharedTask ( )
inline

默认构造函数。

◆ SharedTask() [2/4]

template<class T>
template<class F, class... Args, class = typename std::enable_if< is_callable<F, Args...>::value || is_callable<F, const Event &, Args...>::value>::type>
console::SharedTask< T >::SharedTask ( F && f,
Args &&... args )
inlineexplicit

构造函数,接受一个可调用对象和参数,启动异步任务。

Template Parameters
F可调用对象的类型。
Args参数的类型。
Parameters
f可调用对象。
args参数。
Note
若需传递引用,需使用 std::ref/cref 包装。

◆ SharedTask() [3/4]

template<class T>
console::SharedTask< T >::SharedTask ( const SharedTask< T > & other)
inline

拷贝构造函数。

Parameters
other要拷贝的 SharedTask 对象。

◆ SharedTask() [4/4]

template<class T>
console::SharedTask< T >::SharedTask ( SharedTask< T > && other)
inlinenoexcept

移动构造函数。

Parameters
other要移动的 Task 对象。

◆ ~SharedTask()

template<class T>
console::SharedTask< T >::~SharedTask ( )
default

析构函数。

Member Function Documentation

◆ cancel()

template<class T>
void console::SharedTask< T >::cancel ( )
inline

取消异步任务。

停止线程并设置异常,以表示任务已被取消。

Warning
在调用 cancel 前,应确保已经 start,否则将触发未定义行为。

◆ event_deleter()

template<class T>
void console::SharedTask< T >::event_deleter ( Event * e)
inlinestaticprivate

事件删除器,用于在 std::shared_ptr 销毁时自动调用。

Parameters
e事件指针。

◆ get()

template<class T>
T console::SharedTask< T >::get ( )
inline

获取异步任务的返回值。

Returns
异步任务的返回值。

◆ operator=() [1/2]

template<class T>
SharedTask & console::SharedTask< T >::operator= ( const SharedTask< T > & other)
inline

拷贝赋值运算符。

Parameters
other要拷贝的 SharedTask 对象。
Returns
拷贝后的 SharedTask 对象的引用。

◆ operator=() [2/2]

template<class T>
SharedTask & console::SharedTask< T >::operator= ( SharedTask< T > && other)
inlinenoexcept

移动赋值运算符。

Parameters
other要移动的 Task 对象。
Returns
移动后的 Task 对象的引用。

◆ start() [1/2]

template<class T>
template<class F, class... Args>
std::enable_if< is_callable< F, constEvent &, Args... >::value >::type console::SharedTask< T >::start ( F && f,
Args &&... args )
inline

◆ start() [2/2]

template<class T>
template<class F, class... Args>
std::enable_if<!is_callable< F, constEvent &, Args... >::value >::type console::SharedTask< T >::start ( F && f,
Args &&... args )
inline

启动异步任务。

Template Parameters
F要执行的函数类型。
Args要传递给函数的参数类型。
Parameters
f要执行的函数。
args要传递给函数的参数。
Note
若需传递引用,需使用 std::ref/cref 包装。

◆ status()

template<class T>
std::future_status console::SharedTask< T >::status ( ) const
inline

获取异步任务的状态。

Returns
异步任务的状态。

◆ thread_deleter()

template<class T>
void console::SharedTask< T >::thread_deleter ( std::thread * t)
inlinestaticprivate

线程删除器,用于在 std::shared_ptr 销毁时自动调用。

Parameters
t线程指针。

◆ valid()

template<class T>
bool console::SharedTask< T >::valid ( )
inline

检查异步任务是否有效。

Returns
若任务有效则返回 true,否则返回 false。

◆ wait()

template<class T>
void console::SharedTask< T >::wait ( ) const
inline

等待异步任务完成。

◆ wait_for() [1/3]

template<class T>
template<class Rep, class Period>
std::future_status console::SharedTask< T >::wait_for ( const std::chrono::duration< Rep, Period > & timeout) const
inline

等待异步任务完成,最多等待指定时间。

Template Parameters
Rep时间单位的类型。
Period时间单位的精度。
Parameters
timeout等待的时间。
Returns
异步任务的状态。

◆ wait_for() [2/3]

template<class T>
std::future_status console::SharedTask< T >::wait_for ( double seconds) const
inline

等待异步任务完成,最多等待指定时间。

Parameters
seconds等待的时间 (秒)。
Returns
异步任务的状态。

◆ wait_for() [3/3]

template<class T>
std::future_status console::SharedTask< T >::wait_for ( Time timeout) const
inline

等待异步任务完成,最多等待指定时间。

Parameters
timeout等待的时间。
Returns
异步任务的状态。

◆ wait_until()

template<class T>
template<class Clock, class Duration>
std::future_status console::SharedTask< T >::wait_until ( const std::chrono::time_point< Clock, Duration > & time_point) const
inline

等待异步任务完成,最多等待指定时间。

Parameters
time_point等待的时间点。
Returns
异步任务的状态。

Member Data Documentation

◆ event_

template<class T>
std::shared_ptr<Event> console::SharedTask< T >::event_
private

事件标志

◆ flag_

template<class T>
std::shared_ptr<std::once_flag> console::SharedTask< T >::flag_
private

取消标志

◆ future_

template<class T>
std::shared_future<T> console::SharedTask< T >::future_
private

任务的返回值

◆ thread_

template<class T>
std::shared_ptr<std::thread> console::SharedTask< T >::thread_
private

任务执行的线程


The documentation for this class was generated from the following file: