2.了解boost::bind
使用boost::bind封装一个函数,考虑如下例子
示例2aios
#include <iostream> #include <boost/bind.hpp> void F1() { std::cout << __FUNCTION__ << std::endl; } int main( int argc, char * argv[] ) { boost::bind( &F1 ); return 0; }
运行代码无输出,这是由于咱们建立一个函数触发对象,可是没有实际调用。咱们须要使用()操做符调用函数.
示例2b多线程
#include <iostream> #include <boost/bind.hpp> void F1() { std::cout << __FUNCTION__ << std::endl; } int main( int argc, char * argv[] ) { boost::bind( &F1 )(); return 0; }
如今运行示例,将输出文本。下面示例介绍如何传输参数
示例2C异步
#include <iostream> #include <boost/bind.hpp> void F2( int i, float f ) { std::cout << "i: " << i << std::endl; std::cout << "f: " << f << std::endl; } int main( int argc, char * argv[] ) { boost::bind( &F2, 42, 3.14f )(); return 0; }
运行程序将输出预期的文本。函数
下个示例显示bind类成员函数
示例2dthis
#include <iostream> #include <boost/bind.hpp> class MyClass { public: void F3( int i, float f ) { std::cout << "i: " << i << std::endl; std::cout << "f: " << f << std::endl; } }; int main( int argc, char * argv[] ) { MyClass c; boost::bind( &MyClass::F3, &c, 42, 3.14f )(); return 0; }
咱们必须传递类对象的地址以便调用。若是是在类内部调用,则调用this指针或者shared_from_this().
在多线程中,io_service做为全局对象。在实际应用中,这种作法是不推荐的。若是咱们尝试应用bind io_service对象,则会发生错误,由于io_service不能被拷贝,因此咱们须要使用
shred_ptr.
示例2e线程
#include <boost/asio.hpp> #include <boost/shared_ptr.hpp> #include <boost/thread.hpp> #include <boost/bind.hpp> #include <iostream> void WorkerThread( boost::shared_ptr< boost::asio::io_service > io_service ) { std::cout << "Thread Start\n"; io_service->run(); std::cout << "Thread Finish\n"; } int main( int argc, char * argv[] ) { boost::shared_ptr< boost::asio::io_service > io_service( new boost::asio::io_service ); boost::shared_ptr< boost::asio::io_service::work > work( new boost::asio::io_service::work( *io_service ) ); std::cout << "Press [return] to exit." << std::endl; boost::thread_group worker_threads; for( int x = 0; x < 4; ++x ) { worker_threads.create_thread( boost::bind( &WorkerThread, io_service ) ); } std::cin.get(); io_service->stop(); worker_threads.join_all(); return 0; }
异步程序中,须要确认全局和共享数据的同步访问。下列示例示范了mutex对象的使用方法。
示例2f指针
#include <boost/asio.hpp> #include <boost/shared_ptr.hpp> #include <boost/thread.hpp> #include <boost/thread/mutex.hpp> #include <boost/bind.hpp> #include <iostream> boost::mutex global_stream_lock; void WorkerThread( boost::shared_ptr< boost::asio::io_service > io_service ) { global_stream_lock.lock(); std::cout << "[" << boost::this_thread::get_id() << "] Thread Start" << std::endl; global_stream_lock.unlock(); io_service->run(); global_stream_lock.lock(); std::cout << "[" << boost::this_thread::get_id() << "] Thread Finish" << std::endl; global_stream_lock.unlock(); } int main( int argc, char * argv[] ) { boost::shared_ptr< boost::asio::io_service > io_service( new boost::asio::io_service ); boost::shared_ptr< boost::asio::io_service::work > work( new boost::asio::io_service::work( *io_service ) ); global_stream_lock.lock(); std::cout << "[" << boost::this_thread::get_id() << "] Press [return] to exit." << std::endl; global_stream_lock.unlock(); boost::thread_group worker_threads; for( int x = 0; x < 4; ++x ) { worker_threads.create_thread( boost::bind( &WorkerThread, io_service ) ); } std::cin.get(); io_service->stop(); worker_threads.join_all(); return 0; }
此类mutex对象不可递归锁定。若是递归锁定将形成死锁。对象