用atomic
#include <atomic> #include <iostream> int getNextSerial() { static std::atomic<uint64_t> i; return i++; } int main() { for (int a = 0; a < 34; ++a) { std::cout << getNextSerial() << std::endl; } }
原文:https://www.cnblogs.com/bigben0123/p/14807599.html