我正在使用 boost 并且希望这样:
// --- some random function ---
boost::asio::io_service io;
boost::asio::ip::tcp::socket sock;
char b[256];
// connect and stuff here
boost::asio::async_read( sock,
boost::asio::buffer(b, 256),
boost::bind( &onRead, _1, _2)
);
是一样的
// --- some random function ---
boost::asio::io_service io;
boost::asio::ip::tcp::socket sock;
boost::thread *read_thread;
char b[256];
// connect and stuff here
read_thread = new boost::thread(
boost::bind( &boost::asio::io_service::run, &( io))
);
io.post( &read, b, sock);
// --- read function
bool read( char b[], boost::asio::ip::tcp::socket sock){
boost::asio::read( sock,
boost::asio::buffer(b, 256),
boost::bind( &onRead, _1, _2)
);
}
我只是想在异步调用中发生什么以及它与同步有什么不同。
编辑:我的主要问题是:异步调用阻塞了与 io_service 绑定的线程?
编辑2:这解决了我的问题:http ://www.ibm.com/developerworks/linux/library/l-async/?ca=dgr-lnxw02aUsingPOISIXAIOAPI