c++ - 如何使用 boost::latch?

标签 c++ multithreading boost

我试图在我的程序中使用 boost::latch 来阻止等待,直到所有线程完成或超时。我的代码如下。 ctpl是从https://github.com/vit-vit/CTPL采用的线程池库.

#include <boost/thread/latch.hpp>
#include <CTPL/ctpl.h>
#include <mutex>
#include <iostream>

using namespace std;

int main(int argc, char **argv) {

    ctpl::thread_pool outer_tp(100);
    ctpl::thread_pool inner_tp(5, 5000);

    auto out_func = [&inner_tp](int outer_id, int outer_invoke_idx) {
        int num_batch = 20;
        boost::latch latch_(num_batch);

        auto func = [&latch_, &outer_invoke_idx](int inner_id, int inner_invoke_idx) {
            try {
                std::cout << "outer: " << outer_invoke_idx << ", inner: " << inner_invoke_idx << endl;
            } catch (exception &ex) { cout << "error: " << ex.what() << endl; }
            latch_.count_down();
        };

        for (int i = 0; i < num_batch; ++i) {
            inner_tp.push(func, i);
        }

        latch_.wait_for(boost::chrono::milliseconds(1));
    };

    for (int i = 0; i < 5000; ++i) outer_tp.push(out_func, i);
    outer_tp.stop(true);

    return 0;
}

g++ -std=c++11 test.cpp -lboost_system -lpthread -lboost_chrono -lboost_thread

但是我收到以下错误消息。

 bool boost::latch::count_down(boost::unique_lock&): Assertion `count_ > 0' failed.

如果我使用 latch_.wait() 而不是 latch_.wait_for() 或设置很长的等待时间,代码可以正常运行。因此,我猜想“超时”会导致此错误问题。有谁知道如何修复错误。

最佳答案

您的代码似乎没有什么问题。我认为通过引用在内部线程中引用 latch_ 就是其中之一。将其替换为 shared_ptrboost::latch 可以修复该问题。另一个问题与 outer_invoke_idx 类似。修复这些问题并等待 inner_tp 完成似乎可以让您的测试工作正常。

这是适合我的修改后的测试用例:

#include <boost/thread/latch.hpp>
#include <memory>
#include <CTPL/ctpl.h>
#include <mutex>
#include <iostream>

using namespace std;

int main(int argc, char **argv) {

    ctpl::thread_pool outer_tp(100);
    ctpl::thread_pool inner_tp(5, 5000);

    auto out_func = [&inner_tp](int outer_id, int outer_invoke_idx) {
        int num_batch = 20;
        auto latch_ = std::make_shared<boost::latch>(num_batch);

        auto func = [latch_, outer_invoke_idx](int inner_id, int inner_invoke_idx) {
            try {
                std::cout << "outer: " << outer_invoke_idx << ", inner: " << inner_invoke_idx << endl;
            } catch (exception &ex) { cout << "error: " << ex.what() << endl; }
            latch_->count_down();
        };

        for (int i = 0; i < num_batch; ++i) {
            inner_tp.push(func, i);
        }

        latch_->wait_for(boost::chrono::milliseconds(1));
    };

    for (int i = 0; i < 5000; ++i) outer_tp.push(out_func, i);

    outer_tp.stop(true);
    inner_tp.stop(true);
    std::cout << "EXITING!!!" << std::endl;
    return 0;
}

关于c++ - 如何使用 boost::latch?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46169293/

相关文章:

c++ - 具有私有(private)构造函数和自身静态数组的类

java - 等待主线程直到 ExecutorService 的所有线程池任务完成?

java - 使用多线程迭代列表

c# - 在 PostgreSQL 中锁定表

c++ - 安全跨线程信号/槽 C++

c++ - strstr() 正常吗?

c++ - 如何检查指针指向 iOS 上的堆或堆栈内存?

C++ 重载输出运算符

c++ - 带有 boost 变体的递归 using 声明

c++ - Boost PTree 仅用于读取文件还是用于存储值?