• boost asio 学习(二)了解boost::bind


    2.了解boost::bind
    使用boost::bind封装一个函数,考虑以下例子
    示例2a

    #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类成员函数
    示例2d

    #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对象不可递归锁定。如果递归锁定将造成死锁。

  • 相关阅读:
    win7 删除Windows服务的方法
    如何对SQL Server 2005进行设置以允许远程连接(转载)
    MySql实现远程连接
    OpenCV训练分类器制作xml文档
    Ms SQL Server 约束和规则
    PowerDesigner实用技巧小结(4)
    企业级技术解决方案:hbase+es
    ES的聚合操作
    es之过滤器
    es之得分(加权)
  • 原文地址:https://www.cnblogs.com/itdef/p/5273358.html
Copyright © 2020-2023  润新知