It is possible to simultaneously receive a UDP socket from one remote endpoint and send it to another remote endpoint. However, in the Boost.Asio Threads and Boost.Asio documentation , it is generally unsafe to make concurrent calls for a single object.
Thus, it is safe:
thread_1 | thread_2
--------------------------------------+---------------------------------------
socket.async_receive_from( ... ); |
socket.async_send_to( ... ); |
:
thread_1 | thread_2
--------------------------------------+---------------------------------------
socket.async_receive_from( ... ); |
| socket.async_send_to( ... );:
thread_1 | thread_2
--------------------------------------+---------------------------------------
socket.async_receive_from( ... ); | socket.async_send_to( ... );
|, , boost::asio::async_read, .
, , :
- ,
io_service::run() . async_receive_from async_send_to . , ReadHandler, async_receive_from, async_send_to, WriteHandler, async_send_to, async_receive_from.
void read()
{
socket.async_receive_from( ..., handle_read ); --.
} |
.-----------------------------------------------'
| .----------------------------------------.
V V |
void handle_read( ... ) |
{ |
socket.async_send_to( ..., handle_write ); --. |
} | |
.-------------------------------------------' |
| |
V |
void handle_write( ... ) |
{ |
socket.async_receive_from( ..., handle_read ); --'
}
, , , . boost:: asio:: io_service:: strand , Boost.Thread mutex.
. , buffer endpoint request- > process- > . async_receive_from . , boost:: shared_ptr. , , , , .
, socket_base::reuse_address , . , , , :