C++?boost?thread庫(kù)用法詳細(xì)講解
一、說明
boost::thread的六種使用方法總結(jié),本文初步介紹線程的函數(shù)、構(gòu)造、執(zhí)行的詳細(xì)解釋。
二、boost::thread的幾個(gè)函數(shù)
| 函數(shù) | 功能 |
|---|---|
| join() | 讓主進(jìn)程等待子線程執(zhí)行完畢后再繼續(xù)執(zhí)行 |
| get_id() | 獲得線程的 id 號(hào) |
| detach() | 標(biāo)線程就成為了守護(hù)線程,駐留后臺(tái)運(yùn)行 |
| bool joinable() | 是否已經(jīng)啟動(dòng),為 join() |
thread::join()是個(gè)簡(jiǎn)單暴力的方法,主線程等待子進(jìn)程期間什么都不能做,一般情形是主線程創(chuàng)建thread object后做自己的工作而不是簡(jiǎn)單停留在join上。
thread::join()還會(huì)清理子線程相關(guān)的內(nèi)存空間,此后thread object將不再和這個(gè)子線程相關(guān)了,即thread object不再joinable了,所以join對(duì)于一個(gè)子線程來說只可以被調(diào)用一次,為了實(shí)現(xiàn)更精細(xì)的線程等待機(jī)制,可以使用條件變量等機(jī)制。
1、可會(huì)合(joinable):這種關(guān)系下,主線程需要明確執(zhí)行等待操作,在子線程結(jié)束后,主線程的等待操作執(zhí)行完畢,子線程和主線程會(huì)合,這時(shí)主線程繼續(xù)執(zhí)行等待操作之后的下一步操作。主線程必須會(huì)合可會(huì)合的子線程。在主線程的線程函數(shù)內(nèi)部調(diào)用子線程對(duì)象的wait函數(shù)實(shí)現(xiàn),即使子線程能夠在主線程之前執(zhí)行完畢,進(jìn)入終止態(tài),也必須執(zhí)行會(huì)合操作,否則,系統(tǒng)永遠(yuǎn)不會(huì)主動(dòng)銷毀線程,分配給該線程的系統(tǒng)資源也永遠(yuǎn)不會(huì)釋放。
2、相分離(detached):表示子線程無需和主線程會(huì)合,也就是相分離的,這種情況下,子線程一旦進(jìn)入終止?fàn)顟B(tài),這種方式常用在線程數(shù)較多的情況下,有時(shí)讓主線程逐個(gè)等待子線程結(jié)束,或者讓主線程安排每個(gè)子線程結(jié)束的等待順序,是很困難或不可能的,所以在并發(fā)子線程較多的情況下,這種方式也會(huì)經(jīng)常使用。
三、構(gòu)造
boost::thread有兩個(gè)構(gòu)造函數(shù):
(1)thread():構(gòu)造一個(gè)表示當(dāng)前執(zhí)行線程的線程對(duì)象;
(2)explicit thread(const boost::function0<void>& threadfunc):
boost::function0<void>可以簡(jiǎn)單看為:一個(gè)無返回(返回void),無參數(shù)的函數(shù)。這里的函數(shù)也可以是類重載operator()構(gòu)成的函數(shù);該構(gòu)造函數(shù)傳入的是函數(shù)對(duì)象而并非是函數(shù)指針,這樣一個(gè)具有一般函數(shù)特性的類也能作為參數(shù)傳入,在下面有例子。
#include <boost/thread/thread.hpp>
#include <iostream>
void hello()
{
std::cout <<
"Hello world, I''m a thread!"
<< std::endl;
}
int main(int argc, char* argv[])
{
boost::thread thrd(&hello);
thrd.join();
return 0;
}執(zhí)行結(jié)果:

第二種情況:類重載operator()構(gòu)成的函數(shù)創(chuàng)建線程
#include <boost/thread/thread.hpp>
#include <boost/thread/mutex.hpp>
#include <iostream>
boost::mutex io_mutex;
struct count
{
count(int id) : id(id) { }
void operator()()
{
for (int i = 0; i < 10; ++i)
{
boost::mutex::scoped_lock
lock(io_mutex);
std::cout << id << ": "
<< i << std::endl;
}
}
int id;
};
int main(int argc, char* argv[])
{
boost::thread thrd1(count(1));
boost::thread thrd2(count(2));
thrd1.join();
thrd2.join();
return 0;
} 運(yùn)算結(jié)果:

第三種情況:在類內(nèi)部對(duì)static函數(shù)創(chuàng)建線程
#include <boost/thread/thread.hpp>
#include <iostream>
class HelloWorld
{
public:
static void hello()
{
std::cout <<
"Hello world, I''m a thread!"
<< std::endl;
}
static void start()
{
boost::thread thrd( hello );
thrd.join();
}
};
int main(int argc, char* argv[])
{
HelloWorld::start();
return 0;
} 注意:在這里start()和hello()方法都必須是static方法。因?yàn)閟tatic方法要比main方法提前加載,所以static方法不能調(diào)用一般方法,進(jìn)一步說,static方法無法調(diào)用比它晚加載的方法,切記。 調(diào)用時(shí)用HelloWorld::start(),說明start在定義類的時(shí)候就已經(jīng)ready,無需在實(shí)例化后再調(diào)用。
第四種情況:使用boost::bind函數(shù)創(chuàng)建線程
#include <boost/thread/thread.hpp>
#include <boost/bind.hpp>
#include <iostream>
#include <boost/function.hpp>
class HelloWorld
{
public:
void hello()
{
std::cout <<
"Hello world, I''m a thread!"
<< std::endl;
}
void start()
{
boost::function0< void> f = boost::bind(&HelloWorld::hello, this);
//或boost::function<void()> f = boost::bind(&HelloWorld::hello,this);
boost::thread thrd(f);
thrd.join();
}
};
int main(int argc, char* argv[])
{
HelloWorld hello;
hello.start();
return 0;
}1)定義函數(shù)指針 boost::function0< void> f
2)該指針與HelloWorld::hello函數(shù)綁定, boost::bind(&HelloWorld::hello, this);
3)線程與函數(shù)指針綁定 boost::thread thrd(f);
4)按照常規(guī),將對(duì)象實(shí)例化后,調(diào)用類函數(shù)。
運(yùn)算結(jié)果:

第五種情況:在Singleton模式內(nèi)部創(chuàng)建線程
#include <boost/thread/thread.hpp>
#include <boost/bind.hpp>
#include <iostream>
class HelloWorld
{
public:
void hello()
{
std::cout <<
"Hello world, I''m a thread!"
<< std::endl;
}
static void start()
{
boost::thread thrd( boost::bind
(&HelloWorld::hello,&HelloWorld::getInstance() ) ) ;
thrd.join();
}
static HelloWorld& getInstance()
{
if ( !instance )
instance = new HelloWorld;
return *instance;
}
private:
HelloWorld(){}
static HelloWorld* instance;
};
HelloWorld* HelloWorld::instance = 0;
int main(int argc, char* argv[])
{
HelloWorld::start();
return 0;
} 此例將類對(duì)象實(shí)例化放在類內(nèi)部函數(shù)中,因此無需顯式實(shí)例化,就可以調(diào)用類內(nèi)函數(shù)。值得研究消化。
第六種情況:在類外用類內(nèi)部函數(shù)創(chuàng)建線程
#include <boost/thread/thread.hpp>
#include <boost/bind.hpp>
#include <string>
#include <iostream>
class HelloWorld
{
public:
void hello(const std::string& str)
{
std::cout <<str<< std::endl;
}
};
int main(int argc, char* argv[])
{
HelloWorld obj;
boost::thread thrd( boost::bind(&HelloWorld::hello,&obj,"Hello
world, I''m a thread!" ) ) ;
thrd.join();
return 0;
} 線程如何帶參數(shù)定義?本例就是參考方法!

到此這篇關(guān)于C++ boost thread庫(kù)用法詳細(xì)講解的文章就介紹到這了,更多相關(guān)C++ boost thread內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C++如何計(jì)算二進(jìn)制數(shù)中1的個(gè)數(shù)
這篇文章主要介紹了C++如何計(jì)算二進(jìn)制數(shù)中1的個(gè)數(shù),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-07-07
C語(yǔ)言模擬實(shí)現(xiàn)動(dòng)態(tài)通訊錄
本文主要介紹了C語(yǔ)言模擬實(shí)現(xiàn)動(dòng)態(tài)通訊錄,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-07-07
C語(yǔ)言深入講解指針與結(jié)構(gòu)體的使用
指針提供了對(duì)地址操作的一種方法,因此,使用指針可使得C語(yǔ)言能夠更高效地實(shí)現(xiàn)對(duì)計(jì)算機(jī)底層硬件的操作。另外,通過指針可以更便捷地操作數(shù)組。C數(shù)組允許定義可存儲(chǔ)相同類型數(shù)據(jù)項(xiàng)的變量,結(jié)構(gòu)是C編程中另一種用戶自定義的可用的數(shù)據(jù)類型,它允許您存儲(chǔ)不同類型的數(shù)據(jù)項(xiàng)2022-05-05
在matlab中實(shí)現(xiàn)for循環(huán)的方法
for循環(huán)用來循環(huán)處理數(shù)據(jù),break用于終止離它最近的一層for循環(huán),continue用于跳過離它最近的一層for循環(huán),接著執(zhí)行下一次循環(huán),本文重點(diǎn)給大家介紹在matlab中實(shí)現(xiàn)for循環(huán)的方法,感興趣的朋友一起看看吧2021-11-11
C++ txt 文件讀取,并寫入結(jié)構(gòu)體中的操作
這篇文章主要介紹了C++ txt 文件讀取,并寫入結(jié)構(gòu)體中的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-12-12
總結(jié)UNIX/LINUX下C++程序計(jì)時(shí)的方法
本文總結(jié)了下UNIX/LINUX下C++程序計(jì)時(shí)的一些函數(shù)和方法,對(duì)日常使用C++程序的朋友很有幫助,有需要的小伙伴們可以參考學(xué)習(xí),下面一起來看看吧。2016-08-08

