boost教程

来源:互联网 发布:化学软件 编辑:程序博客网 时间:2024/05/17 01:05

由于项目需要,最近在学习boost 类库,在github上创建了一个boost_tutorial项目,欢迎有兴趣的同学丰富。


boost::thread 的用法如下,更多项目请移步boost_tutorial,其它将在github上陆续更新:

[cpp] view plain copy
  1. /* 
  2.  * boost::thread tutorial 
  3.  * 
  4.  * zdd / zddhub@gmail.com 
  5.  * https://github.com/zddhub/boost_tutorial 
  6.  */  
  7.   
  8. #include <iostream>  
  9. #include <string>  
  10.   
  11. using namespace std;  
  12.   
  13. #include <boost/thread.hpp>  
  14.   
  15. //#1  
  16. void run()  
  17. {  
  18.     cout << "Method #1" <<endl;  
  19. }  
  20.   
  21. //#2  
  22. boost::mutex mutex;  
  23. class Count {  
  24.     int _id;  
  25. public:  
  26.     Count(int id):_id(id) {}  
  27.     void operator ()() {  
  28.         for(int i = 0; i < 10; i++) {  
  29.             boost::mutex::scoped_lock lock(mutex);  
  30.             cout << "Count id:" << _id << " " << i <<endl;  
  31.         }  
  32.     }  
  33. };  
  34.   
  35. //#3  
  36. class InnerThread {  
  37. public:  
  38.     static void run() {  
  39.         cout << "class inner thread: run and start must be static." <<endl;  
  40.     }  
  41.   
  42.     static void start() {  
  43.         boost::thread thread(run);  
  44.         thread.join();  
  45.     }  
  46. };  
  47.   
  48. class InnerThread2 {  
  49. public:  
  50.     void run() {  
  51.         cout << "class inner thread: use boost::bind" <<endl;  
  52.     }  
  53.   
  54.     void start() {  
  55.         boost::function0<void> f = boost::bind(&InnerThread2::run, this);  
  56.         boost::thread thread(f);  
  57.         thread.join();  
  58.     }  
  59. };  
  60.   
  61. //#4  
  62. class SingletonThread {  
  63. public:  
  64.     void run() {  
  65.         cout << "Singleton thread" <<endl;  
  66.     }  
  67.   
  68.     void start() {  
  69.         boost::function0<void> f = boost::bind(&SingletonThread::run, SingletonThread::getInstance());  
  70.         boost::thread thread(f);  
  71.         thread.join();  
  72.     }  
  73.   
  74.     static SingletonThread* getInstance() {  
  75.         if(!_instance) {  
  76.             _instance = new SingletonThread;  
  77.         }  
  78.         return _instance;  
  79.     }  
  80. private:  
  81.     SingletonThread() {}  
  82.     static SingletonThread* _instance;  
  83. };  
  84. SingletonThread* SingletonThread::_instance = 0;  
  85.   
  86. //#5  
  87. class Class {  
  88. public:  
  89.     void run(const std::string& str) {  
  90.         cout <<str <<endl;  
  91.     }  
  92.   
  93.     int add(int x, int y) {  
  94.         cout << "x+y=" <<x+y<<endl;  
  95.         return x+y;  
  96.     }  
  97. };  
  98.   
  99. //#6  
  100. int add(int x, int y) {  
  101.     return x+y;  
  102. }  
  103.   
  104. int main()  
  105. {  
  106.     cout << "boost::thread: " << endl;  
  107.   
  108.     //#1: Simple method  
  109.     boost::thread th1(&run);  
  110.     cout << "main thread " <<endl;  
  111.     th1.join();  
  112.   
  113.     //#2: Through class operator() method  
  114.     boost::thread th21(Count(1));  
  115.     boost::thread th22(Count(2));  
  116.     th21.join();  
  117.     th22.join();  
  118.   
  119.     //#3: Class inner thread : static method  
  120.     InnerThread ith;  
  121.     ith.start();  
  122.     InnerThread::start();  
  123.   
  124.     InnerThread2 ith2;  
  125.     ith2.start();  
  126.   
  127.     //#4: Singleton thread  
  128.     SingletonThread *st = SingletonThread::getInstance();  
  129.     st->start();  
  130.   
  131.     //#5: Outer class thread  
  132.     Class cls;  
  133.     boost::thread outer_thread(boost::bind(&Class::run, &cls, "Outer thread"));  
  134.     outer_thread.join();  
  135.   
  136.     //#6: Complex method: with return value  
  137.     boost::function2<intintint> f = &add;  
  138.     boost::packaged_task<int> pt(boost::bind<int>(f, 1, 2));  
  139.     boost::unique_future<int> fi = pt.get_future();  
  140.     boost::thread th2(boost::move(pt));  
  141.     fi.wait();  
  142.     th2.join();  
  143.     cout <<"x+y="<<fi.get()<<endl;  
  144.   
  145.     boost::packaged_task<int> pt_cls(boost::bind<int>(&Class::add, &cls, 1, 2));  
  146.     boost::unique_future<int> fi_cls = pt_cls.get_future();  
  147.     boost::thread th2_cls(boost::move(pt_cls));  
  148.     fi_cls.wait();  
  149.     th2_cls.join();  
  150.     cout <<"x+y="<<fi_cls.get()<<endl;  
  151.   
  152.     /* 
  153.      *related functions 
  154.      *  boost::thread 
  155.      *  boost::function 
  156.      *  boost::bind 
  157.      */  
  158.   
  159.     //some usages:  
  160.     int x = 1, y =2;  
  161.     cout << "x+y=" <<boost::bind<int>(f, _1, _2)(x,y)<<endl;  
  162.   
  163.   
  164.     return 0;  
  165. }  

原创粉丝点击