【问题标题】:C++ thread pool using boost::asio::thread_pool, why can't I reuse my threads?使用 boost::asio::thread_pool 的 C++ 线程池,为什么我不能重用我的线程?
【发布时间】:2021-12-28 13:33:14
【问题描述】:

我正在尝试使用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();
    }
}

程序输出

0

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

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

请让我知道我哪里出错了,提前谢谢!

【问题讨论】:

    标签: c++ multithreading boost threadpool boost-asio


    【解决方案1】:

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

    这解决了这个问题:

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

    附录#1

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

    Live On Coliru

    #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();
    }
    

    只有一种可能的输出:

    (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)
    

    附录 #2:序列化任务

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

    Live On Coliru

    #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();
    }
    

    有一个可能的输出:

    (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)
    

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

    • 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 for tasks to be finished(我在其中发布了两个答案,一个是在我发现 fork-join 执行器示例之后)。

    【讨论】:

    • 感谢您的回答!这确实解决了它。但是,如果我只想在前一个线程完成后启动下一个线程怎么办?或者boost::asio::thread_pool 可能不是合适的工具?
    • 线程池的目的是完全避免启动线程。如果您想序列化池上的任务,请使用 strand。
    • 我针对 cme​​ts (/cc @S.M.) 阐述了一些其他想法
    猜你喜欢
    • 2012-08-26
    • 2021-09-18
    • 1970-01-01
    • 2011-12-18
    • 1970-01-01
    • 2017-10-25
    • 2012-12-27
    • 2014-01-31
    • 1970-01-01
    相关资源
    最近更新 更多