Chinaunix首页 | 论坛 | 博客
  • 博客访问: 892033
  • 博文数量: 299
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 2493
  • 用 户 组: 普通用户
  • 注册时间: 2014-03-21 10:07
个人简介

Linux后台服务器编程。

文章分类

全部博文(299)

文章存档

2015年(2)

2014年(297)

分类: C/C++

2014-10-31 09:37:24

最近在做一个消息中间件里面涉及到多线程编程,由于跨平台的原因我采用了boost线程库。在创建线程时遇到了几种线程创建方式现总结如下:  
  首先看看boost::thread的构造函数吧,boost::thread有两个构造函数: 
(1)thread():构造一个表示当前执行线程的线程对象; 
(2)explicit thread(const boost::function0& threadfunc): 
     boost::function0可以简单看为:一个无返回(返回void),无参数的函数。这里的函数也可以是类重载operator()构成的函数;该构造函数传入的是函数对象而并非是函数指针,这样一个具有一般函数特性的类也能作为参数传入,在下面有例子。


第一种方式:最简单方法 
#include  
#include  
  
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  
#include  
#include  
  
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
#include  
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
#include
#include  
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
#include
#include  
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
#include
#include
#include  
class HelloWorld
{
public:
 void hello(const std::string& str)
 {
        std::cout <  }
}; 
  
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结合起来使用。

阅读(1594) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~