Last active
August 29, 2015 14:08
-
-
Save mcobzarenco/628bea8eff2fe5abc96b to your computer and use it in GitHub Desktop.
Order by time without duplicates
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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