Skip to content

Instantly share code, notes, and snippets.

@mcobzarenco
Last active August 29, 2015 14:08
Show Gist options
  • Save mcobzarenco/628bea8eff2fe5abc96b to your computer and use it in GitHub Desktop.
Save mcobzarenco/628bea8eff2fe5abc96b to your computer and use it in GitHub Desktop.
Order by time without duplicates
inline int64_t get_monotonous_click() {
static std::atomic<int64_t> last_click{0};
int64_t timestamp{
std::chrono::system_clock::now().time_since_epoch()
/ std::chrono::microseconds(1)};
int64_t old_click{last_click.load()};
int64_t new_click{0};
do {
new_click = std::max(timestamp, old_click + 1);
} while (!std::atomic_compare_exchange_weak(
&last_click, &old_click, new_click));
return new_click;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment