TThread.CurrentThread.ThreadID on Linux

Does anyone have any experience with TThread.CurrentThread.ThreadID on Linux?

I’m getting the same ThreadID, with a different PID, when running an apache module i.e.

LogMessage( 'Init on ’ + inttostr(TThread.CurrentThread.ThreadID) + ’ / ’ + inttostr(getpid()) );

Results in the same startup session:
Init on 140117091985152 / 8582
Init on 140117091985152 / 8583

hsvandrew:

Thread ID is not unique, and is only useful in assorted calls that relate to the thread. Use gettid() to get a system unique thread ID.

If it’s not unique, how can assorted calls that relate to the thread be guaranteed to refer to that thread? Seems illogical

How exactly do you learn this kind of thing? For example, I always thought it was unique on windows and GetCurrentThreadId function (processthreadsapi.h) - Win32 apps | Microsoft Learn seems to suggest it to be the case.

For me, these are the dangers of a multi-target code base where something still works but behaves completely differently.

And I’m sure you didn’t get any help from the article System.Classes.TThread.ThreadID - RAD Studio API Documentation

1 Like

@davidn @hsvandrew

See pthreads(7) - Linux manual page.

Each of the threads in a process has a unique thread identifier (stored in the type *pthread_t*). This identifier is returned to the caller of [pthread_create(3)](https://man7.org/linux/man-pages/man3/pthread_create.3.html), and a thread can obtain its own thread identifier using [pthread_self(3)](https://man7.org/linux/man-pages/man3/pthread_self.3.html). Thread IDs are guaranteed to be unique only within a process. (In all pthreads functions that accept a thread ID as an argument, that ID by definition refers to a thread in the same process as the caller.)