How can I determine if pthread_self is the main (first) thread in this process?

background: I am working on a logging library that is used by many programs.
I assign a human-readable name to each thread, the main thread should be "main", but I would like to be able to detect this state from the library without requiring code at the beginning of each main () function,

Also note: the library code will not always be entered first from the main stream.

+7
c ++ linux pthreads
Feb 01 '11 at 20:55
source share
3 answers

This can be done, depending on the platform on which you are located, but absolutely not in a portable and general form ...

Mac OS X seems to be the only one with a direct and documented approach according to their pthread.h file:

/* returns non-zero if the current thread is the main thread */ int pthread_main_np(void); 

I also found that FreeBSD has a pthread_np.h header that defines pthread_main_np (), so this should work with FreeBSD (at least 8.1), and OpenBSD (at least 4.8) has pthread_main_np () defined in pthread.h too, Please note that _np clearly stands for intolerance!

Otherwise, the only more β€œgeneral" approach that comes to mind is to compare the PID of the process with the TID of the current thread, if they match, this thread is the main one. This does not necessarily work on all platforms, it depends on whether you really can get a TID at all (you cannot use OpenBSD, for example), and if you do, if it has anything to do with PID at all or if the streaming subsystem has its own account, which is not necessarily related.

I also found that some platforms return constant values ​​as TIDs for the main thread, so you can just check them out.

A brief overview of the platforms I tested:

  • Linux: maybe here , syscall (SYS_gettid) == getpid () is what you want
  • FreeBSD: impossible here, thr_self () seems random and not related to getpid ()
  • OpenBSD: impossible here, no way to get TID
  • NetBSD: maybe here , _lwp_self () always returns 1 for the main thread
  • Solaris: here , pthread_self () always returns 1 for the main thread

So basically you should be able to do this directly on Mac OS X, FreeBSD, and OpenBSD.

You can use the TID == PID approach on Linux.

You can use the TID == 1 approach on NetBSD and Solaris.

Hope this helps, have a good day!

+11
Feb 01 2018-11-11T00:
source share

Call pthread_self () from main () and write the result. Compare future calls to pthread_self () with the stored value to see if you are in the main thread.

+7
01 feb. 2018-11-21T00:
source share

You can use some kind of resource with a common name that allows threads to register a name (possibly a stream identifier map for the name). Your logging system can then put the call into a method that gets the name through the thread identifier in a thread-safe manner.

When a thread dies, remove it from the map to avoid memory leaks.

This method should contain the names of all threads, not just the main ones.

0
01 Feb '11 at 21:05
source share



All Articles