Skip to content

Instantly share code, notes, and snippets.

@drodil
Last active January 17, 2019 16:27
Show Gist options
  • Save drodil/cad29e87baa54814786e258c78176abd to your computer and use it in GitHub Desktop.
Save drodil/cad29e87baa54814786e258c78176abd to your computer and use it in GitHub Desktop.
C++ human-readable thread id
#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;
}
@minh0722
Copy link

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment