Skip to content

Instantly share code, notes, and snippets.

@drodil
Last active September 28, 2018 08:12
Show Gist options
  • Save drodil/a9c65f91c76c437badba45ea9eb06151 to your computer and use it in GitHub Desktop.
Save drodil/a9c65f91c76c437badba45ea9eb06151 to your computer and use it in GitHub Desktop.
C++ human-readable thread id
#include <mutex>
#include <thread>
#include <unordered_map>
std::size_t get_thread_id() noexcept {
static std::size_t thread_idx = 0;
static std::mutex thread_mutex;
static std::unordered_map<std::thread::id, std::size_t> thread_ids;
std::lock_guard<std::mutex> lock(thread_mutex);
std::thread::id id = std::this_thread::get_id();
auto iter = thread_ids.find(id);
if (iter == thread_ids.end()) {
iter = thread_ids.insert(std::pair<std::thread::id, std::size_t>(id, thread_idx++)).first;
}
return iter->second;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment