c++ - How to use boost::latch? -
i trying use boost::latch in program block waiting until threads finish or time out. code follows. ctpl thread pool library adopted 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(); }; (int = 0; < num_batch; ++i) { inner_tp.push(func, i); } latch_.wait_for(boost::chrono::milliseconds(1)); }; (int = 0; < 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
however following error message.
bool boost::latch::count_down(boost::unique_lock&): assertion `count_ > 0' failed.
if use latch_.wait() instead of latch_.wait_for() or set long wait time, code works without error. hence guess 'time out' leads error issue. 1 know how fix error.
Comments
Post a Comment