我同时向 boost::asio::thread_pool 提交多个任务。但官方文档并没有讲boost::asio::thread_pool的线程安全。
boost 库版本是 1.69。我的代码如下:
/*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);
所以我想知道代码是否能够在多个线程上工作
最佳答案
boost::asio::post
使用执行器将任务发布到线程池中。执行器要求在this link下描述。 。
其中一个句子是
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.
因此您的代码是安全的,您可以从多个线程调用 post
。
关于c++ - 在多个线程上发布任务时 boost::asio::thread_pool 线程安全吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54741187/