#include <pthread.h> int pthread_getname_np(pthread_t thread, char *name);Service Program Name: QP0WPTHR
The pthread_getname_np() function retrieves the name of the thread. The buffer specified by name must be at least 16 characters in length. The returned thread name will be null terminated in the output buffer.
By default, each thread is unnamed. If the pthread_setname_np() function has not been used to set the name of the specified thread, a null string is retrieved into name.
Note: This function is not portable.
None.
If pthread_getname_np() was not successful, the error condition returned usually indicates one of the following errors. Under some conditions, the value returned could indicate an error other than those listed here.
The value specified for the argument is not correct. Either the thread parameter is not a valid thread handle or name is not a valid pointer.
Note: By using the code examples, you agree to the terms of the Code license and disclaimer information.
#define _MULTI_THREADED #include <pthread.h> #include <stdio.h> #include <string.h> #include <unistd.h> void *threadfunc(void *parm) { printf("In the thread.\n"); sleep(45); // allow main program to set the thread name return NULL; } int main(int argc, char **argv) { pthread_t thread; int rc=0; char theName[16]; memset(theName, 0x00, sizeof(theName)); printf("Create thread using the default attributes\n"); rc = pthread_create(&thread, NULL, threadfunc, NULL); if(0 == rc) { rc = pthread_setname_np(thread, "THREADNAMEISTOOLONG"); sleep(10); if(0 == rc) { rc = pthread_getname_np(thread, theName); if(0 == rc) { printf("The thread name is %s.\n", theName); } } rc = pthread_join(thread, NULL); } if(0 != rc) { printf("An error occurred - %d\n", rc); } printf("Main completed\n"); return(rc); }Example Output:
Create thread using the default attributes The thread name is THREADNAMEISTOO. In the thread. Main completed
[ Back to top | Pthread APIs | APIs by category ]