
Making sure the parent thread gave the pthread a name.įprintf(stdout, "I am thread with ID '%lu', my name is '%s' and my parent gave me my name\n", tid, thread_name ) Ĭonst int setname_rv = pthread_setname_np(thread_info. Added an artificial delay for the sake of the example. #include is needed for the resolution of unsigned int sleep(unsigned int seconds) The next code, creates a pthread and the parent gives the thread a meaningful name. A limitation exists for the minimal stack size that can be created. RTOS tasks created with xTaskCreate() pass their created stack into port.c to allow the pthread library to use that stack with pthreadattrsetstack. The pthread_join() function suspends execution of the calling thread until the target thread terminates, unless the target thread has already terminated.Ĭonst int join_rv = pthread_join(thread_info.thread_id, NULL) Įxample 2: The parent decides for the pthread name pthreadcreate( &tid, &attr, startroutine, args ) Creating new tasks.

so we print pthread_t as an unsigned long intįprintf(stdout, "I am thread with ID '%lu', my name is '%s' and I gave me my name by myself\n", tid, thread_name ) Ĭonst int create_rv = pthread_create(&(thread_info.thread_id), NULL, &self_named_thread, (void *) &thread_info) Usually pthread_t is defined as follows: This function always succeeds, returning the calling thread's ID. Struct thread_info_t *thread_info = (struct thread_info_t *) data Ĭonst int setname_rv = pthread_setname_np(thread_info->thread_id, "Tom Hanks") Ĭhar thread_name Ĭonst int getname_rv = pthread_getname_np(thread_info->thread_id, thread_name, MAX_LENGTH_PTHREAD_NAME)

We know that the input data pointer is pointing to a thread_info_t so we are casting it to the right type. This is the thread that will be called by pthread_create() and it will be executed by the new thread. The thread name is a meaningful C language string, whose length is restricted to 16 characters, including the terminating null byte. #include is needed for the resolution of EXIT_SUCCESS warning: implicit declaration of function ‘pthread_getname_np’ warning: implicit declaration of function ‘pthread_setname_np’ #define _GNU_SOURCE is needed for the resolution of the following warnings The following code, creates a pthread which later, it will give itself a meaningful name. Pthread_named_by_parent.c (compressed) (364 downloads)Įxample 1: The pthread decides for its name

Pthread_self_named.c (compressed) (403 downloads) Naming a pthread using meaningful names, can be a very useful feature for debugging multi-threaded applications as it can make your logs very informative.įor this reason, we are presenting two examples demonstrating the use of names in pthreads. Visual Studio 2010 even has (it seems) special debugging logic to debug the ‘parallel tasks’.29 September 2017 in C / C++ tagged _GNU_SOURCE / C/C++ / C++ / errno / pthread_create / pthread_getname_np / pthread_join / pthread_self / pthread_setname_np / pthread_t by Tux Visual Studio 2010 (we’ll have to wait a few days for it).Systems that support this new way of working with threads are:

Instead of starting a thread and then executing some logic in it, in task-based threading you create a task and then ask the ‘threading logic’ to execute the task. In the last year, there is a trend to go to task-based threading (well, I call it that way, I don’t know what the official name is). In practice this isn’t always that easy to use. However, most thread-functions (in the CRT, Win32 or pthread) are all based around the functionality to create threads, synchronize threads and destroy threads. If you use the Win32 (or other non-CRT functions to create threads) the CRT might not be initialized correctly in the new thread, causing all kinds of problem (you can read about it here: ). If you’re using C/C++, try to use the thread functions of the C/C++ runtime.
