Home > OS >  How to get pthread_attr_t of the main thread in c language?
How to get pthread_attr_t of the main thread in c language?

Time:12-30

I want to get the pthread_attr_t of the main thread. Is there any function to achieve this ?

I have look for such function on some websites, for example, https://pubs.opengroup.org/onlinepubs/7908799/xsh/pthread.h.html. But there seems no.

Can anyone help? thanks!

CodePudding user response:

No, there is no simple way to do this, as you have seen.

Instead, you can check your implementation's documentation, which should specify the default or inherited attributes of a process' initial (main()) thread. (For example, in POSIX, the initial thread is joinable.) You can also use the pthread_get* functions to interrogate the initial thread's attributes at run time, but of course you need to know a priori which attributes to look for, since your implementation may have nonstandard attributes.

CodePudding user response:

You can use the family of functions pthread_attr_getxx but as I have been told in the comments, the function did not modify the struct. So it cannot be used. If I don´t find a solution in a while I will delete the comment

CodePudding user response:

stuff you can do with pthread_attr in a thread you can get that info with
pthread_attr_setaffinity_np pthread_getaffinity_np
pthread_attr_setinheritsched Thread already exists, you can get current sched.
pthread_attr_setschedparam pthread_getschedparam
pthread_attr_setschedpolicy pthread_getschedparam
pthread_attr_setscope dunno
pthread_attr_setdetachstate dunno
pthread_attr_setsigmask_np pthread_sigmask
pthread_attr_setguardsize On Linux, parse /proc/*/maps.
pthread_attr_setstack
pthread_attr_setstackaddr
pthread_attr_setstacksize
On Linux, parse /proc/*/maps like glibc does https://github.com/lattera/glibc/blob/master/nptl/pthread_getattr_np.c#L81

Note that in your program you can wrap calls to pthread_attr and pthread_create and store the information yourself.

of the main thread

I would guess the main thread has scope PTHREAD_SCOPE_SYSTEM and surely is not detached.

  • Related