install and use boost::thread

I am describing how to use boost::thread libaray below.

step1. download boost from http://www.boost.org/ , e.g. boost_1_52_0.tar.gz

step2. extract the file boost_1_52_0.tar.gz to boost_1_52_0

step3. cd to the boost file, boost_1_52_0, and exectute sh bootstrap.sh --prefix=$HOME/boost --with-libraries=thread

step4.  run ./b2 to generate the libaries of boost thread

step5. run ./b2 install, so .hpp and libs are in $HOME/boost.

torstan: ls $HOME/boost
include lib

---------------------

Here is a sample code in boost_1_52_0/libs/thread/tutorial.


#include <boost/thread/thread.hpp>
#include <iostream>

class factorial
{
public:
factorial(int x, int& res) : x(x), res(res) { }
void operator()() { res = calculate(x); }
int result() const { return res; }

private:
int calculate(int x) { return x <= 1 ? 1 : x * calculate(x-1); }

private:
int x;
int& res;
};

int main()
{
int result;
factorial f(5, result);
boost::thread thrd(f);
thrd.join();
std::cout << "5! = " << result << std::endl;
}

 

----------------

all:
g++ -c factorial.cpp -Ihttp://www.cnblogs.com/include
g++ -o factorial factorial.o http://www.cnblogs.com/lib/libboost_system.a http://www.cnblogs.com/lib/libboost_thread.a -lpthread
clean:
rm factorial *.o

------------------

torstan: ./factorial
5! = 120

 

posted on 2013-04-07 20:34  Torstan  阅读(273)  评论(0编辑  收藏  举报

导航