chriskohlhoff / asio

Asio C++ Library
http://think-async.com/Asio
4.72k stars 1.19k forks source link

Clarifying the difference between BOOST_ASIO_CONCURRENCY_HINT_UNSAFE and BOOST_ASIO_CONCURRENCY_HINT_UNSAFE_IO #1466

Open redboltz opened 2 months ago

redboltz commented 2 months ago

I've read https://www.boost.org/doc/html/boost_asio/overview/core/concurrency_hint.html

I want to clarify the following two descriptions:


I understand as follows. Am I understand correctly ?

Operation UNSAFE UNSAFE_IO Examples
run operations single single run, run_for, run_until, run_one, run_one_for, run_one_until, poll, and poll_one
the context's associated I/O objects single single socket, timer
other operations on the io_context single multi post, dispatch, defer

Especially I want to confirm what is "other operations on the io_context". My examples are post, dispatch, and defer. I think that I can switch the executor using these function.

// on thread 1
boost::asio::dispatch( // dispatch is called from multiple threads. It is safe when I use UNSAFE_IO but unsafe if I use UNSAFE
  boost::asio::bind_executor(
    io_context_with_unsafe_io.get_executor(), // run() is called on thread 2
    [...] {
       // on thread 2
       socket.async_read_some(...); // io_context_with_unsafe_io associated socket but it is safe 
    }
  )
);

Is that right ?