本文共 1518 字,大约阅读时间需要 5 分钟。
Microsoft Visual Studio 2010 编译通过
/* * boost::thread tutorial * * * */ #include#include "boost/asio.hpp"#include #include using namespace std; #include //#1 void run() { cout << "Method #1" < f = boost::bind(&InnerThread2::run, this); boost::thread thread(f); thread.join(); } }; //#4 class SingletonThread { public: void run() { cout << "Singleton thread" < f = boost::bind(&SingletonThread::run, SingletonThread::getInstance()); boost::thread thread(f); thread.join(); } static SingletonThread* getInstance() { if(!_instance) { _instance = new SingletonThread; } return _instance; } private: SingletonThread() {} static SingletonThread* _instance; }; SingletonThread* SingletonThread::_instance = 0; //#5 class Class { public: void run(const std::string& str) { cout < < start(); //#5: Outer class thread Class cls; boost::thread outer_thread(boost::bind(&Class::run, &cls, "Outer thread")); outer_thread.join(); //#6: Complex method: with return value boost::function2 f = &add; boost::packaged_task pt(boost::bind (f, 1, 2)); boost::unique_future fi = pt.get_future(); boost::thread th2(boost::move(pt)); fi.wait(); th2.join(); cout <<"x+y="< <
转载于:https://blog.51cto.com/liam2199/2108563