日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

(十)boost库之多线程

發布時間:2024/4/11 编程问答 35 豆豆
生活随笔 收集整理的這篇文章主要介紹了 (十)boost库之多线程 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

1、創建線程

使用boost庫可以方便的創建一個線程,并提供最多支持9個參數的線程函數,相對于void*來說,方便了很多,創建線程主要提供了一下3種方式:

線程庫頭文件:#include <boost/thread.hpp>

a、使用全局函數作為線程執行體

void Func(int nCount) { for (int i = 0; i < nCount; i++) { cout << __FUNCTION__ << i << endl; } } int _tmain(int argc, _TCHAR* argv[]) { boost::thread th(Func, 100); //等待線程結束 th.join(); }

?

b、使用成員函數作為線程執行體

class A { public: void Func(int nCount) { for (int i = 0; i < nCount; i++) { cout << __FUNCTION__ << i << endl; } } }; //線程參數都采用值傳遞,因此即使如下傳入一個臨時變量作為參數,線程照樣可以正確運行 //如果需要傳遞引用,可通過ref庫來實現 boost::thread *pth; void TestThread() { A a; //線程綁定一個局部變量 pth = new boost::thread( &A::Func, &a, 100); }

?

c、仿函數作為線程執行體

class B { public: B(int n):nMem(n) { } void operator()() { for (int i = 0; i < nMem; i++) { cout << __FUNCTION__ << i << endl; } } int nMem; }; //線程thread對象銷毀時,會與線程執行體分離,線程執行體不受影響 void TestThread2() { //創建臨時線程對象 boost::thread(B(100)); }

?

結合以上方法,我們可以輕而易舉的就創建一個線程,結合boost.bind庫和lambda表達式,將會更方便。如:

boost::thread th3([](int nCount){ for (int i = 0; i < nCount; i++) { cout << __FUNCTION__ << i << endl; } }, 10);

2、中斷線程

線程不是在任意時刻都可以被中斷,因此要實現中斷,需要我們自己決定什么時候可以被中斷,boost庫定義了以下函數是可以被中斷的:

  • boost::thread::join()
  • boost::thread::timed_join()
  • boost::boost::thread::try_join_for(),
  • boost::boost::thread::try_join_until(),
  • boost::condition_variable::wait()
  • boost::condition_variable::timed_wait()
  • boost::condition_variable::wait_for()
  • boost::condition_variable::wait_until()
  • boost::condition_variable_any::wait()
  • boost::condition_variable_any::timed_wait()
  • boost::condition_variable_any::wait_for()
  • boost::condition_variable_any::wait_until()
  • boost::thread::sleep()
  • boost::this_thread::sleep_for()
  • boost::this_thread::sleep_until()
  • boost::this_thread::interruption_point()

成員函數interrupt,運行正在執行的線程中斷,被中斷的線程會拋出異常類boost::thread_interrupted,程序應該自行處理該異常,以確保線程正確結束。

void interrupt_thread(int nCount) { try { for (int i = 0; i < nCount; i++) { //sleep函數允許中斷 boost::this_thread::sleep(boost::posix_time::seconds(1)); cout << __FUNCTION__ << i << endl; } } catch(boost::thread_interrupted&) { cout << "thread interrupt" << endl; } } boost::thread th2(interrupt_thread, 100); boost::this_thread::sleep(boost::posix_time::seconds(4)); th2.interrupt();

在以上中斷函數中,除了最后一個,其它都是等待函數,如果想在非等待情況下,運行線程被中斷,就可以使用最后一個函數。

比如我們可以將上面的boost::this_thread::sleep(boost::posix_time::seconds(1));替換成? boost::this_thread::interruption_point();

?

3、線程組

有時我們需要管理一組線程對象,進行統一的等待處理,使用boost::thread_group可以輕松的處理。

#include <boost/bind.hpp> void ThreadGroup() { boost::thread_group grp; grp.create_thread( boost::bind(Func, 10)); A a; grp.add_thread(new boost::thread(&A::Func, &a, 100)); grp.add_thread(new boost::thread(B(100))); grp.join_all(); }

總結

以上是生活随笔為你收集整理的(十)boost库之多线程的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。