Re: [asio-users] Accessing an object not from its io_service thread
Brought to you by:
chris_kohlhoff
From: Christopher K. <ch...@ko...> - 2010-03-30 07:16:04
|
Igor R wrote: > Hello, > > Most of asio objects (like sockets and deadline_timer) are not > thread-safe, as stated in the documentation. However, the docs do not > explicitly refer to the question, whether it's safe to access these > objects from a thread, other than the one(s) that is running their > io_serivce -- like this: > > // pseudo code > static io_service io; > { > tcp::socket sock(io); > boost::thread(&io_service::run, &io); > async_write(sock, someBuffer, handler); > async_read(sock, someBuffer, handler); > } > { > deadline_timer timer(io); > timer.expires_from_this(...); > timer.async_wait(...); > } This is safe. > In other words, does io_service change its objects' state in the way > that might produce race condition with operations initiated by user? No it doesn't. The idea is that the unit of "thread safety" is the I/O object. This page: http://www.boost.org/doc/libs/1_42_0/doc/html/boost_asio/overview/core/basics.html shows that only operation initiation involves the thread-unsafe I/O object. After initiation, only the io_service is involved in the operation, and the io_service is a thread-safe object. Cheers, Chris |