如何开始使用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
posted on 2010-09-15 11:16  VRS  阅读(4545)  评论(1编辑  收藏  举报