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對象不可遞歸鎖定。若是遞歸鎖定將形成死鎖。對象