boost::thread的六种使用方法总结

 

个人分类: c++

boost::thread有两个构造函数: 
(1)thread():构造一个表示当前执行线程的线程对象; 
(2)explicit thread(const boost::function0<void>& threadfunc): 

     boost::function0<void>可以简单看为:一个无返回(返回void),无参数的函数。这里的函数也可以是类重载operator()构成的函数;该构造函数传入的是函数对象而并非是函数指针,这样一个具有一般函数特性的类也能作为参数传入,在下面有例子。

第一种情况:

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

  2. #include <iostream>

  3.  
  4. void hello()

  5. {

  6. std::cout <<

  7. "Hello world, I''m a thread!"

  8. << std::endl;

  9. }

  10.  
  11. int main(int argc, char* argv[])

  12. {

  13. boost::thread thrd(&hello);

  14. thrd.join();

  15. return 0;

  16. }

第二种情况:类重载operator()构成的函数创建线程

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

  2. #include <boost/thread/mutex.hpp>

  3. #include <iostream>

  4.  
  5. boost::mutex io_mutex;

  6.  
  7. struct count

  8. {

  9. count(int id) : id(id) { }

  10.  
  11. void operator()()

  12. {

  13. for (int i = 0; i < 10; ++i)

  14. {

  15. boost::mutex::scoped_lock

  16. lock(io_mutex);

  17. std::cout << id << ": "

  18. << i << std::endl;

  19. }

  20. }

  21.  
  22. int id;

  23. };

  24.  
  25. int main(int argc, char* argv[])

  26. {

  27. boost::thread thrd1(count(1));

  28. boost::thread thrd2(count(2));

  29. thrd1.join();

  30. thrd2.join();

  31. return 0;

  32. }

第三种情况:在类内部对static函数创建线程

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

  2. #include <iostream>

  3. class HelloWorld

  4. {

  5. public:

  6. static void hello()

  7. {

  8. std::cout <<

  9. "Hello world, I''m a thread!"

  10. << std::endl;

  11. }

  12. static void start()

  13. {

  14.  
  15. boost::thread thrd( hello );

  16. thrd.join();

  17. }

  18.  
  19. };

  20. int main(int argc, char* argv[])

  21. {

  22. HelloWorld::start();

  23.  
  24. return 0;

  25. }

  26. 在这里start()和hello()方法都必须是static方法。

第四种情况:使用boost::bind函数创建线程

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

  2. #include <boost/bind.hpp>

  3. #include <iostream>

  4. class HelloWorld

  5. {

  6. public:

  7. void hello()

  8. {

  9. std::cout <<

  10. "Hello world, I''m a thread!"

  11. << std::endl;

  12. }

  13. void start()

  14. {

  15. boost::function0< void> f = boost::bind(&HelloWorld::hello,this);

  16. //或boost::function<void()> f = boost::bind(&HelloWorld::hello,this);

  17. boost::thread thrd( f );

  18. thrd.join();

  19. }

  20.  
  21. };

  22. int main(int argc, char* argv[])

  23. {

  24. HelloWorld hello;

  25. hello.start();

  26. return 0;

  27. }

第五种情况:在Singleton模式内部创建线程

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

  2. #include <boost/bind.hpp>

  3. #include <iostream>

  4. class HelloWorld

  5. {

  6. public:

  7. void hello()

  8. {

  9. std::cout <<

  10. "Hello world, I''m a thread!"

  11. << std::endl;

  12. }

  13. static void start()

  14. {

  15. boost::thread thrd( boost::bind

  16. (&HelloWorld::hello,&HelloWorld::getInstance() ) ) ;

  17. thrd.join();

  18. }

  19. static HelloWorld& getInstance()

  20. {

  21. if ( !instance )

  22. instance = new HelloWorld;

  23. return *instance;

  24. }

  25. private:

  26. HelloWorld(){}

  27. static HelloWorld* instance;

  28.  
  29. };

  30. HelloWorld* HelloWorld::instance = 0;

  31. int main(int argc, char* argv[])

  32. {

  33. HelloWorld::start();

  34. return 0;

  35. }

第六种情况:在类外用类内部函数创建线程

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

  2. #include <boost/bind.hpp>

  3. #include <string>

  4. #include <iostream>

  5. class HelloWorld

  6. {

  7. public:

  8. void hello(const std::string& str)

  9. {

  10. std::cout <<str<< std::endl;

  11. }

  12. };

  13.  
  14. int main(int argc, char* argv[])

  15. {

  16. HelloWorld obj;

  17. boost::thread thrd( boost::bind(&HelloWorld::hello,&obj,"Hello

  18. world, I''m a thread!" ) ) ;

  19. thrd.join();

  20. return 0;

  21. }

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值