<thread>頭文件提供了管理和辨別線程的工具,并且提供函數(shù),可讓當(dāng)前線程休眠。
頭文件內(nèi)容
namespace std
{
class thread;
namespace this_thread
{
thread::id get_id() noexcept;
void yield() noexcept;
template<typename Rep,typename Period>
void sleep_for(
std::chrono::duration<Rep,Period> sleep_duration);
template<typename Clock,typename Duration>
void sleep_until(
std::chrono::time_point<Clock,Duration> wake_time);
}
}
std::thread用來(lái)管理線程的執(zhí)行。其提供讓新的線程執(zhí)行或執(zhí)行,也提供對(duì)線程的識(shí)別,以及提供其他函數(shù)用于管理線程的執(zhí)行。
class thread
{
public:
// Types
class id;
typedef implementation-defined native_handle_type; // optional
// Construction and Destruction
thread() noexcept;
~thread();
template<typename Callable,typename Args...>
explicit thread(Callable&& func,Args&&... args);
// Copying and Moving
thread(thread const& other) = delete;
thread(thread&& other) noexcept;
thread& operator=(thread const& other) = delete;
thread& operator=(thread&& other) noexcept;
void swap(thread& other) noexcept;
void join();
void detach();
bool joinable() const noexcept;
id get_id() const noexcept;
native_handle_type native_handle();
static unsigned hardware_concurrency() noexcept;
};
void swap(thread& lhs,thread& rhs);
可以通過(guò)std::thread::id實(shí)例對(duì)執(zhí)行線程進(jìn)行識(shí)別。
類型定義
class thread::id
{
public:
id() noexcept;
};
bool operator==(thread::id x, thread::id y) noexcept;
bool operator!=(thread::id x, thread::id y) noexcept;
bool operator<(thread::id x, thread::id y) noexcept;
bool operator<=(thread::id x, thread::id y) noexcept;
bool operator>(thread::id x, thread::id y) noexcept;
bool operator>=(thread::id x, thread::id y) noexcept;
template<typename charT, typename traits>
basic_ostream<charT, traits>&
operator<< (basic_ostream<charT, traits>&& out, thread::id id);
Notes
std::thread::id的值可以識(shí)別不同的執(zhí)行,每個(gè)std::thread::id默認(rèn)構(gòu)造出來(lái)的值都不一樣,不同值代表不同的執(zhí)行線程。
std::thread::id的值是不可預(yù)測(cè)的,在同一程序中的不同線程的id也不同。
std::thread::id是可以CopyConstructible(拷貝構(gòu)造)和CopyAssignable(拷貝賦值),所以對(duì)于std::thread::id的拷貝和賦值是沒有限制的。
構(gòu)造一個(gè)std::thread::id對(duì)象,其不能表示任何執(zhí)行線程。
聲明
id() noexcept;
效果
構(gòu)造一個(gè)std::thread::id實(shí)例,不能表示任何一個(gè)線程值。
拋出
無(wú)
NOTE 所有默認(rèn)構(gòu)造的std::thread::id實(shí)例存儲(chǔ)的同一個(gè)值。
比較兩個(gè)std::thread::id的值,看是兩個(gè)執(zhí)行線程是否相等。
聲明
bool operator==(std::thread::id lhs,std::thread::id rhs) noexcept;
返回
當(dāng)lhs和rhs表示同一個(gè)執(zhí)行線程或兩者不代表沒有任何線程,則返回true。當(dāng)lsh和rhs表示不同執(zhí)行線程或其中一個(gè)代表一個(gè)執(zhí)行線程,另一個(gè)不代表任何線程,則返回false。
拋出
無(wú)
比較兩個(gè)std::thread::id的值,看是兩個(gè)執(zhí)行線程是否相等。
聲明
bool operator!=(std::thread::id lhs,std::thread::id rhs) noexcept;
返回
!(lhs==rhs)
拋出
無(wú)
比較兩個(gè)std::thread::id的值,看是兩個(gè)執(zhí)行線程哪個(gè)先執(zhí)行。
聲明
bool operator<(std::thread::id lhs,std::thread::id rhs) noexcept;
返回
當(dāng)lhs比rhs的線程ID靠前,則返回true。當(dāng)lhs!=rhs,且lhs<rhs或rhs<lhs返回true,其他情況則返回false。當(dāng)lhs==rhs,在lhs<rhs和rhs<lhs時(shí)返回false。
拋出
無(wú)
NOTE 當(dāng)默認(rèn)構(gòu)造的std::thread::id實(shí)例,在不代表任何線程的時(shí)候,其值小于任何一個(gè)代表執(zhí)行線程的實(shí)例。當(dāng)兩個(gè)實(shí)例相等,那么兩個(gè)對(duì)象代表兩個(gè)執(zhí)行線程。任何一組不同的std::thread::id的值,是由同一序列構(gòu)造,這與程序執(zhí)行的順序相同。同一個(gè)可執(zhí)行程序可能有不同的執(zhí)行順序。
比較兩個(gè)std::thread::id的值,看是兩個(gè)執(zhí)行線程的ID值是否相等,或其中一個(gè)先行。
聲明
bool operator<(std::thread::id lhs,std::thread::id rhs) noexcept;
返回
!(rhs<lhs)
拋出
無(wú)
比較兩個(gè)std::thread::id的值,看是兩個(gè)執(zhí)行線程的是后行的。
聲明
bool operator>(std::thread::id lhs,std::thread::id rhs) noexcept;
返回
rhs<lhs
拋出
無(wú)
比較兩個(gè)std::thread::id的值,看是兩個(gè)執(zhí)行線程的ID值是否相等,或其中一個(gè)后行。
聲明
bool operator>=(std::thread::id lhs,std::thread::id rhs) noexcept;
返回
!(lhs<rhs)
拋出
無(wú)
將std::thread::id的值通過(guò)給指定流寫入字符串。
聲明
template<typename charT, typename traits>
basic_ostream<charT, traits>&
operator<< (basic_ostream<charT, traits>&& out, thread::id id);
效果
將std::thread::id的值通過(guò)給指定流插入字符串。
返回
無(wú)
NOTE 字符串的格式并未給定。std::thread::id實(shí)例具有相同的表達(dá)式時(shí),是相同的;當(dāng)實(shí)例表達(dá)式不同,則代表不同的線程。
native_handle_type是由另一類型定義而來(lái),這個(gè)類型會(huì)隨著指定平臺(tái)的API而變化。
聲明
typedef implementation-defined native_handle_type;
NOTE 這個(gè)類型定義是可選的。如果提供,實(shí)現(xiàn)將使用原生平臺(tái)指定的API,并提供合適的類型作為實(shí)現(xiàn)。
返回一個(gè)native_handle_type類型的值,這個(gè)值可以可以表示*this相關(guān)的執(zhí)行線程。
聲明
native_handle_type native_handle();
NOTE 這個(gè)函數(shù)是可選的。如果提供,會(huì)使用原生平臺(tái)指定的API,并返回合適的值。
構(gòu)造一個(gè)無(wú)相關(guān)線程的std::thread對(duì)象。
聲明
thread() noexcept;
效果
構(gòu)造一個(gè)無(wú)相關(guān)線程的std::thread實(shí)例。
后置條件
對(duì)于一個(gè)新構(gòu)造的std::thread對(duì)象x,x.get_id() == id()。
拋出
無(wú)
將已存在std::thread對(duì)象的所有權(quán),轉(zhuǎn)移到新創(chuàng)建的對(duì)象中。
聲明
thread(thread&& other) noexcept;
效果
構(gòu)造一個(gè)std::thread實(shí)例。與other相關(guān)的執(zhí)行線程的所有權(quán),將轉(zhuǎn)移到新創(chuàng)建的std::thread對(duì)象上。否則,新創(chuàng)建的std::thread對(duì)象將無(wú)任何相關(guān)執(zhí)行線程。
后置條件
對(duì)于一個(gè)新構(gòu)建的std::thread對(duì)象x來(lái)說(shuō),x.get_id()等價(jià)于未轉(zhuǎn)移所有權(quán)時(shí)的other.get_id()。get_id()==id()。
拋出
無(wú)
NOTE std::thread對(duì)象是不可CopyConstructible(拷貝構(gòu)造),所以該類沒有拷貝構(gòu)造函數(shù),只有移動(dòng)構(gòu)造函數(shù)。
銷毀std::thread對(duì)象。
聲明
~thread();
效果
銷毀*this。當(dāng)*this與執(zhí)行線程相關(guān)(this->joinable()將返回true),調(diào)用std::terminate()來(lái)終止程序。
拋出
無(wú)
將一個(gè)std::thread的所有權(quán),轉(zhuǎn)移到另一個(gè)std::thread對(duì)象上。
聲明
thread& operator=(thread&& other) noexcept;
效果
在調(diào)用該函數(shù)前,this->joinable返回true,則調(diào)用std::terminate()來(lái)終止程序。當(dāng)other在執(zhí)行賦值前,具有相關(guān)的執(zhí)行線程,那么執(zhí)行線程現(xiàn)在就與*this相關(guān)聯(lián)。否則,*this無(wú)相關(guān)執(zhí)行線程。
后置條件
this->get_id()的值等于調(diào)用該函數(shù)前的other.get_id()。oter.get_id()==id()。
拋出
無(wú)
NOTE std::thread對(duì)象是不可CopyAssignable(拷貝賦值),所以該類沒有拷貝賦值函數(shù),只有移動(dòng)賦值函數(shù)。
將兩個(gè)std::thread對(duì)象的所有權(quán)進(jìn)行交換。
聲明
void swap(thread& other) noexcept;
效果
當(dāng)other在執(zhí)行賦值前,具有相關(guān)的執(zhí)行線程,那么執(zhí)行線程現(xiàn)在就與*this相關(guān)聯(lián)。否則,*this無(wú)相關(guān)執(zhí)行線程。對(duì)于*this也是一樣。
后置條件
this->get_id()的值等于調(diào)用該函數(shù)前的other.get_id()。other.get_id()的值等于沒有調(diào)用函數(shù)前this->get_id()的值。
拋出
無(wú)
將兩個(gè)std::thread對(duì)象的所有權(quán)進(jìn)行交換。
聲明
void swap(thread& lhs,thread& rhs) noexcept;
效果
lhs.swap(rhs)
拋出
無(wú)
查詢*this是否具有相關(guān)執(zhí)行線程。
聲明
bool joinable() const noexcept;
返回
如果*this具有相關(guān)執(zhí)行線程,則返回true;否則,返回false。
拋出
無(wú)
等待*this相關(guān)的執(zhí)行線程結(jié)束。
聲明
void join();
先決條件
this->joinable()返回true。
效果
阻塞當(dāng)前線程,直到與*this相關(guān)的執(zhí)行線程執(zhí)行結(jié)束。
后置條件
this->get_id()==id()。與*this先關(guān)的執(zhí)行線程將在該函數(shù)調(diào)用后結(jié)束。
同步
想要在*this上成功的調(diào)用該函數(shù),則需要依賴有joinable()的返回。
拋出
當(dāng)效果沒有達(dá)到或this->joinable()返回false,則拋出std::system_error異常。
將*this上的相關(guān)線程進(jìn)行分離。
聲明
void detach();
先決條件
this->joinable()返回true。
效果
將*this上的相關(guān)線程進(jìn)行分離。
后置條件
this->get_id()==id(), this->joinable()==false
與*this相關(guān)的執(zhí)行線程在調(diào)用該函數(shù)后就會(huì)分離,并且不在會(huì)與當(dāng)前std::thread對(duì)象再相關(guān)。
拋出
當(dāng)效果沒有達(dá)到或this->joinable()返回false,則拋出std::system_error異常。
返回std::thread::id的值來(lái)表示*this上相關(guān)執(zhí)行線程。
聲明
thread::id get_id() const noexcept;
返回
當(dāng)*this具有相關(guān)執(zhí)行線程,將返回std::thread::id作為識(shí)別當(dāng)前函數(shù)的依據(jù)。否則,返回默認(rèn)構(gòu)造的std::thread::id。
拋出
無(wú)
返回硬件上可以并發(fā)線程的數(shù)量。
聲明
unsigned hardware_concurrency() noexcept;
返回
硬件上可以并發(fā)線程的數(shù)量。這個(gè)值可能是系統(tǒng)處理器的數(shù)量。當(dāng)信息不用或只有定義,則該函數(shù)返回0。
拋出
無(wú)
這里介紹一下std::this_thread命名空間內(nèi)提供的函數(shù)操作。
返回std::thread::id用來(lái)識(shí)別當(dāng)前執(zhí)行線程。
聲明
thread::id get_id() noexcept;
返回
可通過(guò)std:thread::id來(lái)識(shí)別當(dāng)前線程。
拋出
無(wú)
該函數(shù)用于通知庫(kù),調(diào)用線程不需要立即運(yùn)行。一般使用小循環(huán)來(lái)避免消耗過(guò)多CPU時(shí)間。
聲明
void yield() noexcept;
效果
使用標(biāo)準(zhǔn)庫(kù)的實(shí)現(xiàn)來(lái)安排線程的一些事情。
拋出
無(wú)
在指定的指定時(shí)長(zhǎng)內(nèi),暫停執(zhí)行當(dāng)前線程。
聲明
template<typename Rep,typename Period>
void sleep_for(std::chrono::duration<Rep,Period> const& relative_time);
效果
在超出relative_time的時(shí)長(zhǎng)內(nèi),阻塞當(dāng)前線程。
NOTE 線程可能阻塞的時(shí)間要長(zhǎng)于指定時(shí)長(zhǎng)。如果可能,逝去的時(shí)間由將會(huì)由一個(gè)穩(wěn)定時(shí)鐘決定。
拋出
無(wú)
暫停指定當(dāng)前線程,直到到了指定的時(shí)間點(diǎn)。
聲明
template<typename Clock,typename Duration>
void sleep_until(
std::chrono::time_point<Clock,Duration> const& absolute_time);
效果
在到達(dá)absolute_time的時(shí)間點(diǎn)前,阻塞當(dāng)前線程,這個(gè)時(shí)間點(diǎn)由指定的Clock決定。
NOTE 這里不保證會(huì)阻塞多長(zhǎng)時(shí)間,只有Clock::now()返回的時(shí)間等于或大于absolute_time時(shí),阻塞的線程才能被解除阻塞。
拋出
無(wú)