In concurrent programming, it is not uncommon that some function is triggered by a certain condition (e.g. a number grow beyond certain threshold). CLUE provides a class concurrent_counter
to implement this. This class in the header file <clue/concurrent_counter.hpp>
.
This class has the following member functions:
Examples: The following example shows how a concurrent counter can be used in practice. In this example, a message will be printed when the accumulated value exceeds 100.
clue::concurrent_counter accum_val(0);
std::thread worker([&](){
for (size_t i = 0; i < 100; ++i) {
accum_val.inc(static_cast<long>(i + 1));
}
});
std::thread listener([&](){
accum_val.wait( clue::gt(100) );
std::printf("accum_val goes beyond 100!\n");
});
worker.join();
listener.join();
The source file examples/ex_cccounter.cpp
provides another example.