Re: [boost] boost asio io_service - not getting boost::asio::error::eof on disconnect in read()

Hi Matthew, Matthew Herrmann <matthew.herrmann@zomojo.com> wrote:
I am calling tcp::socket::read on one thread, and then calling tcp::socket::close on another thread.
Assuming you're trying to do this on the same tcp::socket object, don't do it at all :) It's thread-unsafe and not going to have portable behaviour. If you need cancellation, use the async functions (as it seems you are trying to do). [...]
The io_service::run() call returns when it has no more work to do. Since you have given it no work before starting the thread that calls io_service::run(), it will exit immediately. Starting an async operation (or using io_service::post) is one way to give it work. Another is to construct an object of class io_service::work. E.g.: int main() { boost::asio::io_service io_service; boost::asio::io_service::work work(io_service); // <--- boost::thread(boost::bind( boost::asio::io_service::run, &io_service)); sleep(1); io_service.post(boost::bind(&test_fn)); while(true) { sleep(1); } return 0; } Cheers, Chris
participants (1)
-
Christopher Kohlhoff