Boost logo

Boost :

From: Christopher Kohlhoff (chris_at_[hidden])
Date: 2006-11-05 20:43:08


Hi Matthew, Matthew Herrmann <matthew.herrmann_at_[hidden]> 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). [...] > int main() > { > boost::asio::io_service 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; > } 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


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk