zoukankan      html  css  js  c++  java
  • boost库 线程使用

    最近在做一个消息中间件里面涉及到多线程编程,由于跨平台的原因我采用了boost线程库。在创建线程时遇到了几种线程创建方式现总结如下:  

      首先看看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结合起来使用

    转载说明:https://www.cnblogs.com/lidabo/p/4022715.html

  • 相关阅读:
    代码实现:海滩上有一堆桃子,五只猴子来分。第一只猴子把这堆桃子凭据分为五份,多了一个,这只猴子把多的一个扔入海中,拿走了一份。 第二只猴子把剩下的桃子又平均分成五份,又多了一个,它同样把多的一个扔入海中,拿走了一份, 第三、第四、第五只猴子都是这样做的,问海滩上原来最少有多少个桃子?
    代码实现:编写一个函数,输入n为偶数时,调用函数求1/2+1/4+...+1/n,当输入n为奇数时,调用函数1/1+1/3+...+1/n
    一款炫酷Loading动画--载入成功
    [魅族Degao]Androidclient性能优化
    Spring2.5学习3.2_编码剖析@Resource注解的实现原理
    freemarker写select组件报错总结(六)
    storm笔记:Storm+Kafka简单应用
    2014-8-4阿里电话面试
    UML--组件图,部署图
    CentOS7.1 KVM虚拟化之经常使用管理虚拟机命令(3)
  • 原文地址:https://www.cnblogs.com/bingzzzZZZ/p/8394821.html
Copyright © 2011-2022 走看看