boost thread的六種使用方法總結

2021-08-18 13:26:29 字數 1495 閱讀 7168

boost::thread有兩個建構函式: 

(1)thread():構造乙個表示當前執行執行緒的執行緒物件; 

(2)explicit thread(const boost::function0& threadfunc): 

boost::function0可以簡單看為:乙個無返回(返回void),無引數的函式。這裡的函式也可以是類過載operator()構成的函式;該建構函式傳入的是函式物件而並非是函式指標,這樣乙個具有一般函式特性的類也能作為引數傳入,在下面有例子。

第一種情況:

#include #include void hello()    

int main(int argc, char* argv)

第二種情況:類過載operator()構成的函式建立執行緒

#include #include #include boost::mutex io_mutex; 

struct count

void operator()()

} int id;

};

int main(int argc, char* argv)

第三種情況:在類內部對static函式建立執行緒

#include #include class helloworld

static void start()

}; int main(int argc, char* argv)

在這裡start()和hello()方法都必須是static方法。

第四種情況:使用boost::bind函式建立執行緒

#include #include #include class helloworld

void start()

}; int main(int argc, char* argv)

第五種情況:在singleton模式內部建立執行緒

#include #include #include class helloworld

static void start()

static helloworld& getinstance()

private:

helloworld(){}

static helloworld* instance;

}; helloworld* helloworld::instance = 0;

int main(int argc, char* argv)

第六種情況:在類外用類內部函式建立執行緒

#include #include #include #include class helloworld

};

int main(int argc, char* argv)

boost thread的六種使用方法總結

近期在使用sqlite3作為快取和檔案資料庫,用於儲存micros的落盤資料和實時快取。其中在作為落盤資料庫的時候,為了增加插入效率,我們採用批量插入,但是批量資料 不是直接來自實時快取,我們設計了2級快取ringbuffer,當ringbuffer中的資料快滿的時候,我們才批量寫向檔案資料庫,為此...

使人喜歡你的六種途徑

使人喜歡你的六種途徑 凡不關心別人的人,必會在有生之年遭受重大困難,並且大大傷害別人.也就是這種人導致了人類的種種錯失.如果我們想結交朋友,就要先為別人做些事情 那些需要花時間,精力,體貼,奉獻才能做到的事情.對他人真正充滿興趣.不論你是什麼,或你是誰,你在何處,你在做什麼事,致使你快樂或不快樂的因...

qml multitouch的六種訊號

1.canceled list touchpoints 當由於另一項竊取了觸控事件處理而取消了新的觸控事件時,將發出此訊號。此訊號用於高階用途 當有多個multipointtoucharea處理輸入時,或者在乙個閃爍的內部有乙個multipointtoucharea時,它非常有用。在後一種情況下,如...