M
M
Mercury132021-10-26 17:48:22
C++ / C#
Mercury13, 2021-10-26 17:48:22

Is the code that produces unique numbers based on a timer correctly written?

// timer — общий на всю прогу atomic<long long>
// Можно рассчитывать только на 64-битную архитектуру

long long newTime = someUtcTime();
long long oldTime = timer;
do {
  newTime = std::max(newTime, oldTime + 1);     // должен быть больше того, что имеется
} while (!timer.compare_exchange_strong(&oldTime, newTime))
return (oldTime = newTime);

jcmvbkbc , thanks for the corrections

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question