如何開始使用boost的跨平台thread庫(Linux)
boost首頁:http://www.boost.org/
在首頁點擊download進入sourceforge頁面下載,當前最新版本為boost_1_33_1,有多種檔案格式可供下載(包括.zip, .tar.gz等),內容相同,都是boost_1_33_1的全部原始碼。下載後解壓(假設解壓目錄為/home/yjguo/boost_1_33_1)。
boost中的大部分內容都可以直接原始碼使用,而thread則需要首先編譯出對應的庫。
Linux平台:
Redhat9.0完全預設安裝。
1. 編譯jam(JAM是編譯其他庫的基礎)
進入/home/yjguo/boost_1_33_1/tools/build/jam_src目錄
運行./build.sh即可
運行結束後,將新出現bin.linuxx86目錄,我們所需要的bjam就在該目錄下。
build.sh指令碼自動檢測gcc並調用gcc來編譯bjam。(很多資訊在jam_src目錄下的index.html檔案中都有提到的)
2. 編譯thread庫
進入/home/yjguo/boost_1_33_1目錄
運行./tools/build/jam_src/bin.linuxx86/bjam --with-thread stage (只編譯thread庫)
編譯完成後,結果在/home/yjguo/boost_1_33_1/bin/boost/libs/thread/build目錄下(包括debug/relase, .a/.so等);另外,由於我們在編譯時間使用了stage選項,所以所有的結果都將被拷貝到/home/yjguo/boost_1_33_1/stage/lib目錄下。
3. 準備使用thread庫
選用編譯得到的thread動態庫(.so Share Object)。
將libboost_thread-gcc-mt-1_33_1.so.1.33.1拷貝到/usr/lib/目錄下
將libboost_thread-gcc-mt-d-1_33_1.so.1.33.1拷貝到/usr/lib/目錄下
到/usr/lib目錄下運行
ln -s libboost_thread-gcc-mt-1_33_1.so.1.33.1 libboost_thread-gcc-mt-1_33_1.so
ln -s libboost_thread-gcc-mt-d-1_33_1.so.1.33.1 libboost_thread-gcc-mt-d-1_33_1.so
4. 使用thread庫
在/home/yjguo目錄下建立main.cpp檔案,內容為:
#include <boost/thread/thread.hpp>
#include <iostream>
void hello()
{
std::cout << "Hello world, I'm a thread!" << std::endl;
}
main()
{
boost::thread thrd(&hello);
thrd.join();
}
運行g++ -I /home/yjguo/boost_1_33_1 -pthread -lboost_thread-gcc-mt-1_33_1 main.cpp 得到a.out檔案。
./a.out運行即可。
如何開始使用boost的跨平台thread庫(Windows)boost首頁:http://www.boost.org/在首頁點擊download進入sourceforge頁面下載,當前最新版本為boost_1_44_1,有多種檔案格式可供下載(包括.zip, .tar.gz等),內容相同,都是boost_1_44_1的全部原始碼。下載後解壓(假設解壓目錄為D:\boost\boost_1_44_1)。 boost中的大部分內容都可以直接原始碼使用,而thread則需要首先編譯出對應的庫。
Windows XP平台: 我的機器上安裝了vs2008
1. 編譯jam(JAM是編譯其他庫的基礎)
啟動命令列進入D:\boost\boost_1_44_1\tools\build\jam\src目錄運行build.bat即可運行結束後,將新出現bin.ntx86目錄,我們所需要的bjam.exe就在該目錄下。
2. 編譯thread庫
把bjam.exe複製到D:\boost\boost_1_44_1目錄
啟動命令列進入D:\boost\boost_1_44_1目錄bjam --toolset=msvc-9.0 --with-thread stage (只編譯thread庫)
bjam --toolset=msvc-9.0 --with-date_time stage (同時也需要date_time庫) 編譯完成後,結果在D:\boost\boost_1_44_1\bin\boost\libs\thread\build目錄下(包括debug/relase, dll/lib等);另外,由於我們在編譯時間使用了stage選項,所以所有的結果都將被拷貝到D:\boost\boost_1_ 44 _1\stage\lib目錄下。
3. 準備使用thread庫 選用編譯得到的thread動態庫。
產生的有-1-44和沒有的檔案其實是完全一樣的 將libboost_thread-vc90-mt-gd-1_44.lib
libboost_thread-vc90-mt-1_44.lib
libboost_date_time-vc90-mt-gd-1_44.lib
libboost_date_time-vc90-mt-1_44.lib 將這四個檔案拷貝到VS2008的庫目錄裡,我的是C:\Program Files\Microsoft Visual Studio 9.0\VC\lib
還需要 1.設定Project屬性的Code Geneartion------Runtime Library 為 /MDd 或者 /MD
2.目錄設定。主菜單“Tools”->“Options…”項,彈出的視窗中,按圖所示進行操作,添加boost標頭檔所在的目錄
D:\boost\boost_1_44_1
4. 使用thread庫 在vs2008中建立一個空的Win32 Console Project,代碼:#include <boost/thread/thread.hpp>
#include <iostream>void hello()
{
std::cout << "Hello world, I'm a thread!" << std::endl;
}main()
{
boost::thread thrd(&hello);
thrd.join();
} 編譯運行即可。
另外,總結幾種使用方法:
首先看看boost::thread的建構函式吧,boost::thread有兩個建構函式:
(1)thread():構造一個表示當前執行線程的線程對象;
(2)explicit thread(const boost::function0<void>& threadfunc):
boost::function0<void>可以簡單看為:一個無返回(返回void),無參數的函數。這裡的函數也可以是類重載operator()構成的函數;該建構函式傳入的是函數對象而並非是函數指標,這樣一個具有一般函數特性的類也能作為參數傳入,在下面有例子。
第一種方式:最簡單方法
#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;
}
第二種方式:複雜類型對象作為參數來建立線程:
#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;
}
第三種方式:在類內部建立線程;
(1)類內部靜態方法啟動線程
#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方法。
(2)如果要求start()和hello()方法不能是靜態方法則採用下面的方法建立線程:
#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;
}
void start()
{
boost::function0< 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;
}
(3)在Singleton模式內部建立線程:
#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;
}
第四種方法:用類內建函式在類外部建立線程;
#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;
}
如果線程需要綁定的函數有參數則需要使用boost::bind。比如想使用 boost::thread建立一個線程來執行函數:void f(int i),如果這樣寫:boost::thread thrd(f)是不對的,因為thread建構函式聲明接受的是一個沒有參數且傳回型別為void的型別,而且不提供參數i的值f也無法運行,這時就可以寫:boost::thread thrd(boost::bind(f,1))。涉及到有參函數的綁定問題基本上都是boost::thread、boost::function、boost::bind結合起來使用。
原文:http://weiwu83.javaeye.com/blog/98388
http://dev.firnow.com/course/3_program/c++/cppjs/200856/114821.html