Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set a higher priority to a boost::asio thread wrt the process

I know that C++ has some ways to set priority on threads. Something like what is is being discussed here. Similar to that, is there something similar available in threads that boost::asio runs? I am doing an async_read and async_write to read and write data from the network.

Note that I do want to set priority betweeen the read and write like described here in boost::asio example. I rather want to set a higher priority to the asio reader thread with respect to the whole process.

If boost::asio doesn't provide a direct mechanism to higher the priority of its thread(s), then is possible that I create the std::thread, set a higher priority to it like discussed here and provide it to asio to use it for async_read/async_write? Or is this possible with a strand?

Environment:
I am using boost 1.68 with a C++11 compiler.

like image 608
AdeleGoldberg Avatar asked Jun 09 '26 21:06

AdeleGoldberg


1 Answers

I'm with the commenters that doubt this will be useful.

But to answer your question: there is no such thing as a Boost Asio thread.

It is explicitly documented why:

Threads And Boost Asio

Consequently, it is the library user's responsibility to create and manage all threads to which the notifications will be delivered.

The reasons for this approach include:

  • By only calling io_context::run() from a single thread, the user's code can avoid the development complexity associated with synchronisation. For example, a library user can implement scalable servers that are single-threaded (from the user's point of view).
  • A library user may need to perform initialisation in a thread shortly after the thread starts and before any other application code is executed. For example, users of Microsoft's COM must call CoInitializeEx before any other COM operations can be called from that thread.
  • The library interface is decoupled from interfaces for thread creation and management, and permits implementations on platforms where threads are not available.

So, you create your own threads - except the main thread I suppose - and can do so in whatever fashion you require.

like image 106
sehe Avatar answered Jun 11 '26 17:06

sehe



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!