12

How can i check if the current thread is the main thread on linux? It looks like gettid() only returns an pid but it seems that linux does not guarantee the thread with main() always has a const and uniform pid.

The reason for this is that I have an automatic parallelization going on and I want to make sure pthread_create() is not called in a function that is already running on a thread that's created by pthread_create().

alk
  • 69,737
  • 10
  • 105
  • 255
user2958862
  • 233
  • 4
  • 8

2 Answers2

13

For Linux:

If getpid() returns the same result as gettid() it's the main thread.

int i_am_the_main_thread(void)
{
  return getpid() == gettid();
}

From man gettid:

gettid() returns the caller's thread ID (TID). In a single-threaded process, the thread ID is equal to the process ID (PID, as returned by getpid(2)). In a multithreaded process, all threads have the same PID, but each one has a unique TID.

From man clone:

Thread groups were a feature added in Linux 2.4 to support the POSIX threads notion of a set of threads that share a single PID. Internally, this shared PID is the so-called thread group identifier (TGID) for the thread group. Since Linux 2.4, calls to getpid(2) return the TGID of the caller.

The threads within a group can be distinguished by their (system-wide) unique thread IDs (TID). A new thread's TID is available as the function result returned to the caller of clone(), and a thread can obtain its own TID using gettid(2).

alk
  • 69,737
  • 10
  • 105
  • 255
  • 2
    Thanks! Btw a side note on the linux machine i'm using gettid() is not defined. I found in another post which says using `#include syscall(SYS_gettid);` to replace get gettid() and now it's working! – user2958862 Dec 13 '13 at 02:50
  • Storing the pthread_self id value is magnitude faster because this requires two slow system calls. – Lothar Aug 14 '20 at 08:53
  • @Lothar: The values returned by `pthread_self()` and `gettid()` most likely are not the same. On Linux they definitely are not. – alk Aug 14 '20 at 13:47
4

What about using pthread_self()?.

This returns the thread_id of the calling thread. With this function, you can store the main thread id (when you know is main) and compare it later with other values returned from pthread_self() to identify if they are the main thread or another one.

Although I think is wiser to have well structured code. Something like functions to be executed in slave threads and other functions to be executed in the master thread. This is a better approach to this kind of problems.

Paulo Bu
  • 29,294
  • 6
  • 74
  • 73
  • The reason is that like I put in the original post I'm doing **automatic** parallelization which means I can not assume how an original given program looks like, so I need runtime check to see which thread that specific pthread_create() may be on. – user2958862 Dec 12 '13 at 01:21
  • that's why i need to check the current thread and not go with the easier route of writing a nicely organized program – user2958862 Dec 12 '13 at 01:35
  • @user2958862: Yes I tested and it is a very clean way to do it. I'm glad you could solve your problem :) – Paulo Bu Dec 13 '13 at 14:25