dolphin icon indicating copy to clipboard operation
dolphin copied to clipboard

Common::CurrentThreadId() : linux implementation

Open fenugrec opened this issue 3 years ago • 2 comments

POSIX pthreads doesn't provide a portable way to get a thread ID that can readily be cast to an int. But linux provides gettid() which returns a pid_t : a signed integer type [...] In the GNU C Library, this is an int

fenugrec avatar Jul 14 '22 00:07 fenugrec

What do you want to achieve/fix by doing that? AFAICT, that doesn't seem like the proper way to do it according to the gettid man:

The thread ID returned by this call is not the same thing as a POSIX thread ID (i.e., the opaque value returned by pthread_self(3)).

It also seems the linux code base uses pthread_self to determine the thread id in most of the Common's helper functions. IIRC, many of these functions were used before C++ supported std::thread properly. Can't std::this_thread::get_id be used now? We don't use pthread_create directly even if some std::thread implementations might use it.

sepalani avatar Jul 14 '22 06:07 sepalani

Those are good points; I may have misunderstood the purpose of CurrentThreadId() , I thought it was just "produce a unique int value to distinguish threads between them" - I only found it used in logging functions ?

If c++ std::thread now provides the wanted semantics that's probably what's best, but I saw other platform-specific code and assumed the proper std:: support was still lacking. gettid() is sufficient to provide a unique thread ID, but you are correct it cannot be used as an argument to any of the POSIX pthread_* functions.

No problem if this PR isn't in the right direction , feel free to close ! It was just 2 lines after all.

fenugrec avatar Jul 14 '22 12:07 fenugrec