【问题标题】:Is boost::asio::thread_pool thread safe when posting tasks on multiple threads?在多个线程上发布任务时 boost::asio::thread_pool 线程安全吗?
【发布时间】:2019-07-11 11:27:42
【问题描述】:

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

所以我想知道代码是否可以在多个线程上工作

【问题讨论】:

    标签: c++ boost thread-safety asio


    【解决方案1】:

    boost::asio::post 使用执行器将任务发布到线程池中。执行器要求在this link 下进行了描述。 其中一个句子是

    执行器复制构造函数、比较运算符和其他成员 这些要求中定义的功能不应引入数据竞争 由于从不同的地方同时调用这些函数 线程。

    所以你的代码是安全的,你可以从多个线程调用post

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-11-13
      • 2021-09-18
      • 2012-03-05
      • 2023-03-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多