多線程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