August 2017
Intermediate to advanced
438 pages
14h 19m
English
The boost::call_once may pass parameters to the function to call:
#include <iostream>void once_printer(int i) { static boost::once_flag flag = BOOST_ONCE_INIT; boost::call_once( flag, [](int v) { std::cout << "Print once " << v << '\n'; }, i // <=== Passed to lambda from above. ); // ...}
Now, if we call once_printer function in a loop:
int main() { for (unsigned i = 0; i < 10; ++i) { once_printer(i); }}
Only a single line will be in the output:
Print once 0
C++11 has a std::call_once and std::once_flag in the <mutex> header. Unlike the Boost version, the standard library version of the once_flag does not require initialization via a macro, it has a constexpr constructor. As usual, Boost version is usable on pre-C++11 compilers, ...
Read now
Unlock full access