icewall
icewall

Reputation: 51

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

Upvotes: 3

Views: 774

Answers (1)

rafix07
rafix07

Reputation: 20938

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.

Upvotes: 4

Related Questions