#include <iostream>
#include <thread>
#include <mutex>
static std::once_flag g_once_flag;
void do_once() {
std::call_once(g_once_flag, [](){ std::cout << "Do once: called once\n"; });
}
int main() {
std::thread st1(do_once);
std::thread st2(do_once);
std::thread st3(do_once);
std::thread st4(do_once);
st1.join();
st2.join();
st3.join();
st4.join();
}
输出结果(可在代码区点在线编译运行):Do once: called once