使用 boost::asio::thread_pool 的 C++ 线程池,为什么我不能重用我的线程?

Slu*_*ger 3 c++ multithreading boost boost-asio threadpool

我正在尝试boost::asio::thread_pool在我的应用程序中创建线程池。我创建了以下玩具示例,看看我是否理解它是如何工作的,但显然不明白:)

#include <boost/asio/post.hpp>
#include <boost/asio/thread_pool.hpp>
#include <boost/bind.hpp>
#include <iostream>

boost::asio::thread_pool g_pool(10);

void f(int i) {
    std::cout << i << "\n";
}

int main() {
    for (size_t i = 0; i != 50; ++i) {
        boost::asio::post(g_pool, boost::bind(f, 10 * i));
        g_pool.join();
    }
}
Run Code Online (Sandbox Code Playgroud)

程序输出

0
Run Code Online (Sandbox Code Playgroud)

我对两件事感到困惑:第一,如果我正在等待线程完成使用g_pool.join(),为什么我不能在下一次迭代中重用这些线程。10,20,30,...即,我希望还能看到在后续迭代中打印的数字等。

其次,我正在创建一个大小为 10 的线程池,为什么我至少没有看到 10 个输出呢?我无法理解这个问题。

请告诉我哪里出错了,先谢谢了!

seh*_*ehe 6

您在发布第一个任务后加入池。因此,在您接受第二个任务之前,池就会停止。这就解释了为什么你没有看到更多。

这修复了:

for (size_t i = 0; i != 50; ++i) {
    post(g_pool, boost::bind(f, 10 * i));
}
g_pool.join();
Run Code Online (Sandbox Code Playgroud)

附录 #1

回应评论。如果您想等待特定任务的结果,请考虑未来:

住在科里鲁

#include <boost/asio.hpp>
#include <boost/bind/bind.hpp>
#include <boost/thread.hpp>
#include <iostream>
#include <future>

boost::asio::thread_pool g_pool(10);

int f(int i) {
    std::cout << '(' + std::to_string(i) + ')';
    return i * i;
}

int main() {
    std::cout << std::unitbuf;
    std::future<int> answer;

    for (size_t i = 0; i != 50; ++i) {
        auto task = boost::bind(f, 10 * i);
        if (i == 42) {
            answer = post(g_pool, std::packaged_task<int()>(task));
        } else
        {
            post(g_pool, task);
        }
    }

    answer.wait(); // optionally make sure it is ready before blocking get()
    std::cout << "\n[Answer to #42: " + std::to_string(answer.get()) + "]\n";

    // wait for remaining tasks
    g_pool.join();
}
Run Code Online (Sandbox Code Playgroud)

具有一种可能的输出:

(0)(50)(30)(90)(110)(100)(120)(130)(140)(150)(160)(170)(180)(190)(40)(200)(210)(220)(240)(250)(70)(260)(20)(230)(10)(290)(80)(270)(300)(340)(350)(310)(360)(370)(380)(330)(400)(410)(430)(60)(420)(470)(440)(490)(480)(320)(460)(450)(390)
[Answer to #42: 176400]
(280)
Run Code Online (Sandbox Code Playgroud)

附录#2:序列化任务

如果要序列化特定任务,可以使用链。例如,根据参数模 3 的余数序列化所有请求:

住在科里鲁

#include <boost/asio.hpp>
#include <boost/bind/bind.hpp>
#include <boost/thread.hpp>
#include <iostream>
#include <future>

boost::asio::thread_pool g_pool(10);

int f(int i) {
    std::cout << '(' + std::to_string(i) + ')';
    return i * i;
}

int main() {
    std::cout << std::unitbuf;
    
    std::array strands{make_strand(g_pool.get_executor()),
                       make_strand(g_pool.get_executor()),
                       make_strand(g_pool.get_executor())};

    for (size_t i = 0; i != 50; ++i) {
        post(strands.at(i % 3), boost::bind(f, i));
    }

    g_pool.join();
}
Run Code Online (Sandbox Code Playgroud)

可能的输出:

(0)(3)(6)(2)(9)(1)(5)(8)(11)(4)(7)(10)(13)(16)(19)(22)(25)(28)(31)(34)(37)(40)(43)(46)(49)(12)(15)(14)(18)(21)(24)(27)(30)(33)(36)(39)(42)(45)(48)(17)(20)(23)(26)(29)(32)(35)(38)(41)(44)(47)
Run Code Online (Sandbox Code Playgroud)

请注意,所有工作都在任何线程上完成,线程上的任务按照发布的顺序发生。所以,

  • 0、3、6、9、12...
  • 1、4、7、10、13...
  • 2、5、8、11、14...

不过严格来说是连续发生的

  • 4 和 7 不需要发生在同一个物理线程上
  • 11 可能发生在 4 之前,因为它们不在同一条线上

如果您需要更多“类似屏障”的同步,或者所谓的 fork-join 语义,请参阅Boost asio thread_pool join does not wait fortasks to be finish(其中我发布了两个答案,一个是在我发现 fork-join 执行器之后)例子)。