Last active
January 17, 2019 16:27
-
-
Save drodil/cad29e87baa54814786e258c78176abd to your computer and use it in GitHub Desktop.
C++ human-readable thread id
This file contains hidden or 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
#include <thread> | |
#include <atomic> | |
std::size_t get_thread_id() noexcept { | |
static std::atomic<std::size_t> thread_idx{0}; | |
thread_local std::size_t id = thread_idx; | |
thread_idx++; | |
return id; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
shouldn't it be
thread_local std::size_t id = thread_idx++;
In your case isn't
thread_idx
get incremented everytime it is called in the same thread?