日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學(xué)無(wú)先后,達(dá)者為師

網(wǎng)站首頁(yè) 編程語(yǔ)言 正文

C++?boost?thread庫(kù)用法詳細(xì)講解_C 語(yǔ)言

作者:無(wú)水先生 ? 更新時(shí)間: 2022-12-22 編程語(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

欄目分類(lèi)
最近更新