1、線程的基本管控
包含頭文件<thread>
后,通過構(gòu)建std::thread
對象啟動線程,任何可調(diào)用類型都適用于std::thread
。
void do_some_work();
struct BackgroundTask
{
void operator()() const;
};
//空的thread對象,不接管任何線程函數(shù)
std::thread t1;
//傳入普通函數(shù)
std::thread t2(do_some_work);
//傳入lambda函數(shù)
std::thread t3([]() { /*do something*/ });
//傳入可調(diào)用對象
BackgroundTask task;
std::thread t4(task);
//不能使用std::thread t4(BackgroundTask()),雖然本意是傳入臨時變量,但這會被編譯器解釋成函數(shù)聲明。多用一對圓括號或者使用列表初始化可以解決這個問題。
std::thread t5((BackgroundTask()));
std::thread t6{BackgroundTask()};
啟動線程后,需要明確是等待它結(jié)束、還是任由它獨自運行:
- 調(diào)用成員函數(shù)
join()
會先等待線程結(jié)束,然后隸屬于該線程的任何存儲空間都會被清除,std::thread
對象不再關(guān)聯(lián)到已結(jié)束的線程。 - 調(diào)用成員函數(shù)
detach()
會分離線程使其在后臺運行,此后無法獲得與它關(guān)聯(lián)的std::thread
對象。分離線程的歸屬權(quán)和控制權(quán)都轉(zhuǎn)移給了C++運行時庫,線程退出時與之關(guān)聯(lián)的資源會被正確回收。
調(diào)用了join()
或是detach()
之后,其joinable()
方法將返回false
,所以也就不能再調(diào)用join()
。不能對空的std::thread
對象調(diào)用join()
或是detach()
。如果線程啟動后既不調(diào)用join()
也不調(diào)用detach()
,那么當(dāng)std::thread
對象銷毀時,其析構(gòu)函數(shù)將調(diào)用std::terminate()
終止整個程序。
2、向線程函數(shù)傳遞參數(shù)
若需向線程上的函數(shù)傳遞參數(shù),直接向std::thread
的構(gòu)造函數(shù)添加更多參數(shù)即可。線程具有內(nèi)部存儲空間,參數(shù)會按照默認(rèn)方式先復(fù)制到該處,然后這些副本被當(dāng)作臨時變量,以右值形式傳遞給線程上的函數(shù)。即使函數(shù)的相關(guān)參數(shù)按設(shè)想應(yīng)該是引用,上述過程依然會發(fā)生。
void f(int i, const std::string& s);
std::thread t(f, 3, "hello");
上述代碼在新線程上調(diào)用f(3, "hello")
,盡管f()
的第二個參數(shù)是std::string
類型,但字符串內(nèi)容仍然以指針const char*
的形式傳入,直到進(jìn)入新線程的上下文環(huán)境后才轉(zhuǎn)換為std::string
類型。所以如果參數(shù)是指針,需要特別注意其生命周期,否則可能導(dǎo)致嚴(yán)重問題,例如:
void f(int i, const std::string& s);
void oops(int param)
{
char buffer[1024];
sprintf(buffer, "%d", param);
std::thread t(f, 3, buffer);
t.detach();
}
buffer
是指向局部數(shù)組的指針,我們原本設(shè)想buffer
會在新線程內(nèi)轉(zhuǎn)換成std::string
對象,但在此完成之前,oops()
函數(shù)很有可能已經(jīng)退出,導(dǎo)致局部數(shù)組被銷毀從而引發(fā)未定義的行為。這一問題的根源在于:std::thread
的構(gòu)造函數(shù)原樣復(fù)制所提供的值,并未立即將其轉(zhuǎn)換成預(yù)期的參數(shù)類型,等到轉(zhuǎn)換發(fā)生時,指針可能已經(jīng)失效。所以解決方法就是,在buffer
傳入std::thread
的構(gòu)造函數(shù)之前,就先把它轉(zhuǎn)換成std::string
對象:
std::thread t(f, 3, std::string(buffer));
除了指針外,傳遞引用也需要小心。例如我們想要的是非const引用:
void update_widget_data(WidgetData& data);
void oops()
{
WidgetData data;
std::thread t(update_widget_data, data);
t.join();
}
根據(jù)update_widget_data
函數(shù)的聲明,參數(shù)需要以引用的方式傳入,但std::thread
的構(gòu)造函數(shù)對此卻毫不知情,它忽略了函數(shù)所期望的參數(shù)類型,直接復(fù)制了我們提供的值。然而,線程庫的內(nèi)部代碼會把參數(shù)的副本(std::thread
構(gòu)造時由對象data
復(fù)制得出,位于新線程的內(nèi)部存儲空間)以右值的形式傳遞給update_widget_data
函數(shù),所以這段代碼會編譯失敗,因為不能向非const引用傳遞右值。解決方法就是使用std::ref()
函數(shù)加以包裝,這樣傳遞給update_widget_data
函數(shù)的就是指向data的引用,代碼就能編譯成功:
std::thread t(update_widget_data, std::ref(data));
要將某個類的成員函數(shù)設(shè)為線程函數(shù),我們需要給出合適的對象指針作為第一個參數(shù),成員函數(shù)的參數(shù)放在其后的位置。
class X
{
public:
void do_lengthy_work();
}
X my_x;
std:thread t(&X::do_lengthy_work, &my_x);
對于只能移動、不能復(fù)制的對象,傳遞參數(shù)時需要使用std::move()
來轉(zhuǎn)移歸屬權(quán)。在下面的例子中,BigObject
對象的歸屬權(quán)會發(fā)生轉(zhuǎn)移,先進(jìn)入新創(chuàng)建的線程的內(nèi)部存儲空間,再轉(zhuǎn)移給process_big_object()
函數(shù)。
void process_big_object(std::unique_ptr<BigObject>);
std::unique_ptr<BigObject> p(new BigObject);
std::thread t(process_big_object, std::move(p));
3、移交線程歸屬權(quán)
std::thread
不能復(fù)制,但支持移動語義。對于一個具體的執(zhí)行線程,其歸屬權(quán)可以在多個std::thread
實例之間轉(zhuǎn)移。
void some_function();
void some_other_function();
std::thread t1(some_function);
std::thread t2 = std::move(t1); //將線程的歸屬權(quán)顯式地轉(zhuǎn)移給t2
t1 = std::thread(some_other_function); //線程原本與std::thread臨時對象關(guān)聯(lián),其歸屬權(quán)隨即轉(zhuǎn)移給t1
std::thread t3; //按默認(rèn)方式構(gòu)造,未關(guān)聯(lián)任何線程
t3 = std::move(t2); //t2原本關(guān)聯(lián)的線程的歸屬權(quán)轉(zhuǎn)移給t3
//經(jīng)過上面這些轉(zhuǎn)移,t1與運行some_other_function的線程關(guān)聯(lián),t2沒有關(guān)聯(lián)線程,t3與運行some_function的線程關(guān)聯(lián)
t1 = std::move(t3); //在這次轉(zhuǎn)移之時,t1已經(jīng)關(guān)聯(lián)運行some_other_function的線程,因此std::thread的析構(gòu)函數(shù)中會調(diào)用std::terminate(),終止整個程序。所以只要std::thread對象還在管控著一個線程,就不能簡單地向它賦新值。
因為std::thread
支持移動語義,所以只要容器同樣知悉移動意圖,就可以裝載std::thread
對象。因此我們可以寫出下列代碼,生成多個線程,然后等待它們運行完成。
void do_work(unsigned id);
void foo()
{
std::vector<std::thread> threads;
for (unsigned i = 0; i < 20; ++i)
{
threads.push_back(std::thread(do_work, i));
}
for (auto& entry : threads)
{
entry.join();
}
}
4、識別線程
使用C++標(biāo)準(zhǔn)庫的std::thread::hardware_concurrency()
函數(shù)可以獲取系統(tǒng)中邏輯處理器的數(shù)量。如果信息無法獲取,該函數(shù)可能返回0。
線程ID的類型是std::thread::id
,它有兩種獲取方法:文章來源:http://www.zghlxwxcb.cn/news/detail-689474.html
- 在與線程關(guān)聯(lián)的
std::thread
對象上調(diào)用成員函數(shù)get_id()
,即可得到該線程的ID。如果std::thread
對象沒有關(guān)聯(lián)任何執(zhí)行線程,則調(diào)用get_id()
返回的是按默認(rèn)構(gòu)造方式生成的std::thread::id
對象,表示“線程不存在”。 - 當(dāng)前線程的ID可以通過調(diào)用
std::this_thread::get_id()
獲取。
std::thread::id
對象可以支持很多種操作:文章來源地址http://www.zghlxwxcb.cn/news/detail-689474.html
- 可隨意進(jìn)行復(fù)制操作或比較運算。
- 可用作關(guān)聯(lián)容器(
std::set
、std::map
、std::multiset
、std::multimap
)的鍵值,無序關(guān)聯(lián)容器(std::unordered_set
、std::unordered_map
、std::unordered_multiset
、std::unordered_multimap
)的鍵值,或用于排序。 - 寫到輸出流,例如
std::cout << std::this_thread::get_id();
。
到了這里,關(guān)于《C++并發(fā)編程實戰(zhàn)》讀書筆記(1):線程管控的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!