boost::bind II

来源:互联网 发布:淘宝人生txt下载 编辑:程序博客网 时间:2024/06/06 16:27
//1 Use boost::bind not in a Class char buff[1024]; //buff must be global or static, or from a new() memory!void handle_write(boost::asio::serial_port &port,const boost::system::error_code &ec){    port.async_read_some(boost::asio::buffer(buff),        boost::bind(handle_read,boost::ref(port),        boost::asio::placeholders::error,        boost::asio::placeholders::bytes_transferred));}void handle_read(boost::asio::serial_port &port,const boost::system::error_code &ec,size_t byte_read)  //not a fixed format{//See above:  boost::bind()}char cCmd = 0x02;boost::asio::async_write( port,    boost::asio::buffer(&cCmd,1),    boost::bind(handle_write,boost::ref(port),    boost::asio::placeholders::error) );        //2 Use boost::bind in a Class void SerialPort::handle_write(const boost::system::error_code &ec){    m_port.async_read_some(boost::asio::buffer(buff),        boost::bind(&SerialPort::handle_read,this,    //this,        boost::asio::placeholders::error,        boost::asio::placeholders::bytes_transferred));}void SerialPort::handle_read(const boost::system::error_code &ec,size_t byte_read)  //This is a fixed format! must be so?{//See above:  boost::bind()}char cCmd = 0x02;boost::asio::async_write( m_port,  //a member in a class    boost::asio::buffer(&cCmd,1),    boost::bind(&SerialPort::handle_write,this,     //this,     boost::asio::placeholders::error) );


boost::thread 传递参数问题:void TcpTask(boost::asio::io_service &iostcp){    iostcp.run(); //循环分发回调函数}int main(){    boost::asio::io_service iostcp;    boost::thread thread_tcp(boost::bind(&TcpTask, boost::ref(iostcp)));    thread_tcp.join();    return 0;}


0 0
原创粉丝点击