简体   繁体   中英

Is the asio strand object thread safe?

I have to develop an asynchronous client that talks to a server. The client runs in a separate thread from the main application and just reads what the server sends using a callback chain. Each read handler registers the next one through a strand (it is a bit more complex since I use a class method as a callback so I need to bind *this to match the handler's signature):

_socketObject.async_read_some(
        asio::buffer(_recv_buf.data(),_recv_buf.size()),
        asio::bind_executor(_strand, std::bind(
            &Connection::_handleRead, shared_from_this(),
            std::placeholders::_1, std::placeholders::_2)));

To write to the server I'd like the main application to post ( https://think-async.com/Asio/asio-1.16.1/doc/asio/reference/post/overload2.html ) through the same strand a callback that performs the write to the server (this is to avoid concurrent access to the socket and some shared data).

The thing that I want to know is if it is sufficient to copy the strand object used in the client or it is necessary to keep a reference to the original. In the latter case I am concerned about the thread safety of the operation. I'd like to avoid an explicit mutex on the strand object, if possible.

I use the header only version of the library (non-Boost).

Yes. See docs

Thread Safety

Distinct objects : Safe.

Shared objects : Safe.

Strands can be copied. In fact, you can create a new strand off another executor and if that was on a strand it will end up representing the same strand identity.


Additionally, a mutex on a strand couldn't possibly work because composed operations need to dispatch work on the thread, and they would not be aware of the need for locking.

In general locking is a no-no in async tasks: Strands: Use Threads Without Explicit Locking

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM