Reputation: 2257
According to the MSDN doc for closesocket:
"A Winsock client must never issue closesocket on s concurrently with another Winsock function call."
For example, what happens if an IO completion thread detects an error and closes the socket:
client_socket_.lowest_layer().close(ec);
while concurrently another thread is about to call:
async_write(client_socket_,...)
Could both of these calls hit their underlying closesocket() and WSASend() APIs, thus causing a potential crash?
I've seen these types of crashes in ordinary C++, but wasn't sure if Boost C++ had some kind of builtin mechanism to prevent it? If not, then do these calls require a scoped locked mutex etc.,..
Thanks.
Upvotes: 1
Views: 159
Reputation: 393134
If you use a strand¹ there's no real problem, though I'll submit that it's probably cleaner (much cleaner) to invoke shutdown()
before/instead of close()
in many scenarios.
Asio does requires you to synchronize access to the socket/stream objects (everything other than the documented thread-safe objects as io_context
and strand
, really). Strands fulfill the role traditionally held by critical sections.
¹ implicit or explicit, see Why do I need strand per connection when using boost::asio?
Upvotes: 1