boost::thread用法

1

2


不愧是C++,多线程果然是烦!

先记下来:

01 //Lock object
02 boost::mutex m_guard;
03  
04 void worker(const std::string& s)
05 {
06     //outside critical section
07     {
08         //outside critical section
09         boost::lock_guard<boost::mutex> lock_obj(m_guard);    //lock()
10         //inside critical section
11         cout << s << " Enter critical section" << endl;
12         std::cout << s << '\t'
13                   << "Now sleeping...." << endl;
14         boost::this_thread::sleep(boost::posix_time::milliseconds(5000));
15         cout << s << " Quit critical section" << endl;
16         //inside critical section
17     }
18     //outside critical section
19 }
20  
21 int main( ) {
22  
23    std::string s1 = "t1 ";
24    std::string s2 = "t2 ";
25  
26    //two ways to initialize a thread
27    boost::thread thr1(worker,s1);
28    boost::thread thr2(Adapter<WorkerFunPtr, std::string>(worker, s2));
29  
30    //wait thread1 and thread2 to complete their work
31    thr1.join();
32    thr2.join();
33  
34    cout << "All thread end" << endl;
35    system("pause");
36 }

注意到,这个C++lock的方式还是很特别的;其他语言,比如C#\Java,都是内置了lock关键字,以

1 lock
2 {
3     // Critical section
4     ....
5 }

的形式来表明锁住 Critical section,而在Boost中是以申明一个lockguard变量(本例中如此,应该还有其他方式)的形式来申明进入Critical section,并且以这个lockguard变量的消亡来退出Critical section。因此要拿捏好这个变量的生存周期。比如以花括号来控制这个局部变量的生存周期

01 //outside critical section
02 {
03     //outside critical section
04     boost::lock_guard<boost::mutex> lock_obj(m_guard);    //lock()
05     //inside critical section
06     cout << s << " Enter critical section" << endl;
07         .......
08     cout << s << " Quit critical section" << endl;
09     //inside critical section
10 }
11 //outside critical section

非常精巧


 

原文地址:http://www.cppblog.com/janvy/archive/2010/03/25/110498.aspx

最近在做一个消息中间件里面涉及到多线程编程,由于跨平台的原因我采用了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结合起来使用

posted @ 2011-08-06 17:56  wangkangluo1  阅读(15267)  评论(0编辑  收藏  举报