2

I've seen two approaches to get the thread id which return different results. one is used by a third party package I'm using which look like this :

...
#elif __APPLE__
    uint64_t tid;
    pthread_threadid_np(nullptr, &tid);
    return static_cast<size_t>(tid);
#else 
...

While on other places in the project I'm using the standard std way which is inheritedly cross platform

std::this_thread::get_id()

the third-party native approach return 4993544 while the std code return 0x1708b3000

Any idea how to explain the difference?

Zohar81
  • 4,554
  • 5
  • 29
  • 82

1 Answers1

1

The difference is that std::this_thread::get_id() is cross-platform, hence its result is NOT a real id, it's whatever std needs to find that real id.

It's common that by adding many layers of abstraction to a framework, accessing the real things becomes harder or even useless (as none of the framework's API accepts the Real-ID).

Where std is no exception, consider it yet another framework, but built-in of the language - hence should be used more than frameworks (unless you support some very old compilers).

Top-Master
  • 7,611
  • 5
  • 39
  • 71
  • is there a way to convert `std::this_thread::get_id` result to `pthread_threadid_np` result ? – Zohar81 Apr 23 '23 at 10:57
  • @Zohar81 There is no direct conversion from `std::thread::id` to `std::thread::native_handle`, but that's a separate question - **See:** https://stackoverflow.com/q/36865846/8740349 – Top-Master Apr 23 '23 at 11:05