От: | Alexander G | ||
Дата: | 24.11.16 10:58 | ||
Оценка: | 14 (3) |
#include <iostream>
#include <mutex>
struct S
{
static void OnlyLiveOnce() { std::cout << "Once\n"; }
S()
{
std::call_once(f, OnlyLiveOnce);
}
static std::once_flag f;
};
S s; // Called once
std::once_flag S::f; // Initialized once flag
int main()
{
S s1; // Called twice
S s2; // No more calls
}