我怎么知道pthread_self是否是进程中的主线程(第一个)? [英] how can I tell if pthread_self is the main (first) thread in the process?

查看:448
本文介绍了我怎么知道pthread_self是否是进程中的主线程(第一个)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

背景:我正在研究许多程序使用的日志记录库.
我为每个线程分配一个易于理解的名称,主线程应获得"main",但我希望能够从库中检测到该状态,而无需在每个main()函数的开头都添加代码.

background: I'm working on a logging library that is used by many programs.
I'm assigning a human-readable name for each thread, the main thread should get "main", but I'd like to be able to detect that state from within 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 thread.

推荐答案

这是可行的,具体取决于您所使用的平台,但绝对不能以任何可移植且通用的方式...

This is kinda doable, depending on the platform you're on, but absolutely not in any portable and generic way...

根据Mac OS X的

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);

我还发现FreeBSD具有定义pthread_main_np()的pthread_np.h标头,因此它也应在FreeBSD上运行(至少8.1),而OpenBSD(至少4.8)在pthread.h中也定义了pthread_main_np(). .请注意,_np明确表示不可携带!

I also found that FreeBSD has a pthread_np.h header that defines pthread_main_np(), so this should work on FreeBSD too (8.1 at least), and OpenBSD (4.8 at least) has pthread_main_np() defined in pthread.h too. Note that _np explicitly stands for non-portable!

否则,想到的唯一更通用"的方法是将进程的PID与当前线程的TID进行比较,如果它们匹配,则该线程为主要线程. 这不一定在所有平台上都有效,这取决于您是否实际上可以完全获得TID(例如,不能在OpenBSD中获得),以及是否可以,是否与TID完全相关.线程子系统具有自己的计费方式,不一定与之相关.

Otherwise, the only more "general" approach that comes to mind is comparing the PID of the process to the TID of the current thread, if they match, that thread is main. This does not necessarily work on all platforms, it depends on if you can actually get a TID at all (you can't in OpenBSD for example), and if you do, if it has any relation to the PID at all or if the threading subsystem has its own accounting that doesn't necessarily relate.

我还发现某些平台将常量值作为主线程的TID返回,因此您只需检查一下即可.

I also found that some platforms give back constant values as TID for the main thread, so you can just check for those.

我检查过的平台的简要摘要:

A brief summary of platforms I've checked:

  • Linux:可能在此处,您想要的是syscall(SYS_gettid)== getpid()
  • FreeBSD:在这里不可能,thr_self()似乎是随机的,与getpid()无关
  • OpenBSD:在这里不可能,没有办法获得TID
  • NetBSD:可能在此处,_ lwp_self()始终为主线程返回1
  • Solaris:可能在此处,pthread_self()始终为主线程返回1
  • Linux: possible here, syscall(SYS_gettid) == getpid() is what you want
  • FreeBSD: not possible here, thr_self() seems random and without relation to getpid()
  • OpenBSD: not possible here, there is no way to get a TID
  • NetBSD: possible here, _lwp_self() always returns 1 for the main thread
  • Solaris: possible here, pthread_self() always returns 1 for the main thread

因此,基本上,您应该可以直接在Mac OS X,FreeBSD和OpenBSD上进行操作.

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

您可以在Linux上使用TID == PID方法.

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

您可以在NetBSD和Solaris上使用TID == 1方法.

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

我希望这会有所帮助,祝你有美好的一天!

I hope this helps, have a good day!

这篇关于我怎么知道pthread_self是否是进程中的主线程(第一个)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆