简体   繁体   English

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

[英]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.我同时向 boost::asio::thread_pool 提交了多个任务。 But official document does not talk about thread safe of boost::asio::thread_pool.但是官方文档并没有讲到 boost::asio::thread_pool 的线程安全。

the boost lib version is 1.69. boost lib 版本是 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所以我想知道代码是否能够在多个线程上工作

boost::asio::post uses executor to post tasks into thread pool. boost::asio::post使用 executor 将任务发布到线程池中。 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.所以你的代码是安全的,你可以从多个线程调用post

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何处理:如果 boost::asio::post 无休止地重复,当 boost::asio::thread_pool 析构函数被触发时? - How to deal: if boost::asio::post is endlessly repeated, when boost::asio::thread_pool destructor is triggered? C++线程池使用boost::asio::thread_pool,为什么我不能重用我的线程? - C++ thread pool using boost::asio::thread_pool, why can't I reuse my threads? 在 lambda 函数中捕获 boost::asio::thread_pool - Capturing boost::asio::thread_pool in lambda function 协程不分布在 asio::thread_pool 线程上 - Coroutines are not distributed over asio::thread_pool threads 可以使用 boost::asio::thread_pool 而不是将 boost::asio::io_context 与 boost::thread::thread_group 结合使用吗? - Can boost::asio::thread_pool be used instead of combining boost::asio::io_context with a boost::thread::thread_group? boost asio:针对不同任务的不同线程池 - boost asio: different thread pool for different tasks 我的 boost::asio::thread_pool 中的线程 ID 始终相同 - Thread-ID is always the same in my boost::asio::thread_pool 等到发布到 boost::asio::thread_pool 的作业(与所有作业完全相反)完成? - Wait until A job (as starkly opposed to ALL jobs) posted to boost::asio::thread_pool completes? Helgrind 在简单的 boost::asio::thread_pool 程序中报告同步错误 - Helgrind reports synchronization errors in simple boost::asio::thread_pool program asio :: thread_pool甚至在调用构造函数之前就失败了 - asio::thread_pool fails before constructor is even called
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM