c++ - 在多个线程上发布任务时 boost::asio::thread_pool 线程安全吗?

标签 c++ boost thread-safety asio

我同时向 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/

相关文章:

C++ boost : Split function is_any_of()

c++ - 具有夹具支持的 BOOST_DATA_TEST_CASE

c++ - 错误 C2400 : inline assembler syntax error in 'second operand' ; found 'register'

c++ - 从相机投影线

c++ - Boost C++ LNK1104 无法打开文件 'libboost_serialization-vc140-mt-gd-1_62.lib' 错误

winforms - 中止这个文件搜索线程是否安全?

android - Java - 安卓 : Thread being called (run) twice

Java tomcat线程内存使用限制

c++ - 如何在 QT/C++ 应用程序中添加操作项

c++ - fatal error LNK1104 : cannot open file 'SDL2.obj'