Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is boost::asio::thread_pool thread safe when posting tasks on multiple threads?

I'm submitting more than one tasks to boost::asio::thread_pool concurrently. But official document does not talk about thread safe of boost::asio::thread_pool.

the boost lib version is 1.69. And my code like below:

/*define a thread pool*/    
boost::asio::thread_pool pool(4);

//on thread 1
boost::asio::post(pool, my_task_1);
...
//on thread 2
boost::asio::post(pool, my_task_2);

so I want to know is the code able to work on multiple threads

like image 771
icewall Avatar asked Sep 14 '26 08:09

icewall


1 Answers

boost::asio::post uses executor to post tasks into thread pool. Executor requirements are described under this link. One of sentences is

The executor copy constructor, comparison operators, and other member functions defined in these requirements shall not introduce data races as a result of concurrent calls to those functions from different threads.

so your code is safe, you can call post from multiple threads.

like image 193
rafix07 Avatar answered Sep 16 '26 06:09

rafix07