網(wǎng)站首頁(yè) 編程語(yǔ)言 正文
一、說(shuō)明
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è)子線程來(lái)說(shuō)只可以被調(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)銷(xiāo)毀線程,分配給該線程的系統(tǒng)資源也永遠(yuǎn)不會(huì)釋放。
2、相分離(detached):表示子線程無(wú)需和主線程會(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è)無(wú)返回(返回void),無(wú)參數(shù)的函數(shù)。這里的函數(shù)也可以是類(lèi)重載operator()構(gòu)成的函數(shù);該構(gòu)造函數(shù)傳入的是函數(shù)對(duì)象而并非是函數(shù)指針,這樣一個(gè)具有一般函數(shù)特性的類(lèi)也能作為參數(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é)果:
第二種情況:類(lèi)重載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é)果:
第三種情況:在類(lèi)內(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)一步說(shuō),static方法無(wú)法調(diào)用比它晚加載的方法,切記。 調(diào)用時(shí)用HelloWorld::start(),說(shuō)明start在定義類(lèi)的時(shí)候就已經(jīng)ready,無(wú)需在實(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)用類(lèi)函數(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;
}
此例將類(lèi)對(duì)象實(shí)例化放在類(lèi)內(nèi)部函數(shù)中,因此無(wú)需顯式實(shí)例化,就可以調(diào)用類(lèi)內(nèi)函數(shù)。值得研究消化。
第六種情況:在類(lèi)外用類(lèi)內(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ù)定義?本例就是參考方法!
原文鏈接:https://yamagota.blog.csdn.net/article/details/127892727
相關(guān)推薦
- 2022-08-04 react使用mobx封裝管理用戶登錄的store示例詳解_React
- 2022-03-15 PEM_read_bio_X509_AUX() failed (SSL: error:0906D06
- 2022-11-20 C++遞歸算法處理島嶼問(wèn)題詳解_C 語(yǔ)言
- 2023-02-15 Qt槽函數(shù)會(huì)被執(zhí)行多次的問(wèn)題原因及解決方法_C 語(yǔ)言
- 2023-02-09 go?slice?擴(kuò)容實(shí)現(xiàn)原理源碼解析_Golang
- 2022-03-16 使用Nginx實(shí)現(xiàn)端口轉(zhuǎn)發(fā)TCP代理的實(shí)現(xiàn)示例_nginx
- 2022-09-07 Python+OpenCV實(shí)現(xiàn)圖像識(shí)別替換功能詳解_python
- 2022-07-23 SQL?Server中的邏輯函數(shù)介紹_MsSql
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲(chǔ)小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過(guò)濾器
- Spring Security概述快速入門(mén)
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡(jiǎn)單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對(duì)象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支