多线程API说明:http://www.cplusplus.com/refe...promise
两种类型的多任务处理:基于进程和基于线程。安全
std::thread 在 #include<thread> 头文件中声明,所以使用 std::thread 时须要包含 #include<thread> 头文件。 多线程
包含std::thread类以及std::this_thread命名空间。管理线程的函数和类在 中声明.
< atomic > :包含std::atomic和std::atomic_flag类,以及一套C风格的原子类型和与C兼容的原子操做的函数。并发
thread() _NOEXCEPT { // construct with no thread _Thr_set_null(_Thr); } 建立一个空的 thread 执行对象。
template<class Fn, class... Args> explicit thread(Fn&& fn, Args&&... args); 建立std::thread执行对象,该thread对象可被joinable,新产生的线程会调用threadFun函数,该函数的参数由 args 给出
thread(const thread&) = delete; 拷贝构造函数(被禁用),意味着 thread 不可被拷贝构造。
thread(thread&& x)noexcept move 构造函数,调用成功以后 x 不表明任何 thread 执行对象。 注意:可被 joinable 的 thread 对象必须在他们销毁以前被主线程 join 或者将其设置为 detached。
get_id-API说明:
获取线程ID,返回类型std::thread::id对象。异步
inline thread::id thread::get_id() const _NOEXCEPT { // return id for this thread return (_Thr_val(_Thr)); } Returns the thread id. 返回当前调用的线程ID。 If the thread object is joinable, the function returns a value that uniquely identifies the thread. If the thread object is not joinable, the function returns a default - constructed object of member type thread::id.
joinable-API说明:判断线程是否能够加入等待ide
bool joinable() const _NOEXCEPT { // return true if this thread can be joined return (!_Thr_is_null(_Thr)); } Returns whether the thread object is joinable. 返回线程对象是不是joinable的。 A thread object is joinable if it represents a thread of execution. 若是是一个正在执行的线程,那么它是joinable的。 A thread object is not joinable in any of these cases: 下列任一状况都是非joinable if it was default-constructed. 默认构造器构造的。 if it has been moved from (either constructing another thread object, or assigning to it). 经过移动构造得到的。 if either of its members join or detach has been called. 调用了join或者detach方法的。
inline void thread::join() { // join thread if (!joinable()) _Throw_Cpp_error(_INVALID_ARGUMENT); const bool _Is_null = _Thr_is_null(_Thr); // Avoid Clang -Wparentheses-equality if (_Is_null) _Throw_Cpp_error(_INVALID_ARGUMENT); if (get_id() == _STD this_thread::get_id()) _Throw_Cpp_error(_RESOURCE_DEADLOCK_WOULD_OCCUR); if (_Thrd_join(_Thr, 0) != _Thrd_success) _Throw_Cpp_error(_NO_SUCH_PROCESS); _Thr_set_null(_Thr); } The function returns when the thread execution has completed. 当该线程执行完成后才返回。(即等待子线程执行完毕才继续执行主线程) This synchronizes the moment this function returns with the completion of all the operations in the thread: This blocks the execution of the thread that calls this function until the function called on construction returns (if it hasn't yet). 该函数的返回与子线程执行完毕同步,该函数会阻塞调用该函数的线程直到子线程调用完毕。 After a call to this function, the thread object becomes non-joinable and can be destroyed safely. 调用该函数后,子线程对象变成non-joinable以及能够安全地销毁。
void detach() { // detach thread if (!joinable()) _Throw_Cpp_error(_INVALID_ARGUMENT); _Thrd_detachX(_Thr); _Thr_set_null(_Thr); } Detaches the thread represented by the object from the calling thread, allowing them to execute independently from each other. 将本线程从调用线程中分离出来,容许本线程独立执行。(可是当主进程结束的时候,即使是detach()出去的子线程无论有没有完成都会被强制杀死) Both threads continue without blocking nor synchronizing in any way. Note that when either one ends execution, its resources are released. 两个线程不会堵塞也不会同步,注意他们任一一个结束的时候,所持有的资源将会被释放。 After a call to this function, the thread object becomes non-joinable and can be destroyed safely. 调用该方法后,该线程对象变得不可链接以及能够安全地销毁。
包含了与互斥量相关的类以及其余类型和函数函数
包含两个Provider类(std::promise和std::package_task)和两个Future类(std::future和std::shared_future)以及相关的类型和函数。this
包含与条件变量相关的类,包括std::condition_variable和std::condition_variable_any。atom