Pthread create
lastmod 2019-12-19created 2018-07-181 edits
pthread_create creates a new thread. The new thread is created and runs the start_rutine function, passing it the arg argument. The created thread is removed when it calls pthread_exit or when start_rutine terminates. The attr argument is used to specify thread-related attributes. See pthread_attr_init for details. If attr is NULL, the default attributes are used. On Linux, threads default to joinable and a non real-time scheduling policy.
Syntax
int pthread_create(pthread_t * thread,
const pthread_attr_t * attr,
void * (*start_routine)(void *),
void *arg);
Parameters:
| thread | out | Thread ID |
| attr | in | Thread attributes. (If NULL, the default attributes are used) |
| start_routine | in | Pointer to the function the thread will run |
| arg | in, optional | Argument to the function associated with the thread |
Requirements:
| Header | pthread.h |
| !Library | libpthread.o |
On success, it stores the thread identification number (ID) in the thread identifier thread and returns 0. On failure, it returns a nonzero error code. EAGAIN is returned when there are insufficient resources to create the thread or when PTHREAD_THREADS_MAX is exceeded.
Releasing pthread resources
Typically, a thread is created with pthread_create, and once it is finished, the code that created it calls pthread_join to release the thread's resources and end it. If a created thread is not handled with pthread_join, its resources are not returned even after the thread has terminated. Since such leftover resources are considered a memory leak, pthread_join is essential for synchronization between threads and for releasing resources. However, there is a detach option that lets a created thread return its resources to the system on its own when it terminates, without calling pthread_join. As the name suggests, it seems to mean "let it run independently" (only in terms of resources, of course). There are two ways to apply the detach option.
First, set detach in the attributes of the thread to be created by pthread_create, so that the new thread releases its resources independently.
pthread_t thread;
pthread_attr_t attr;
if( pthread_attr_init(&attr) != 0 )
return 1;
if( pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED) != 0 )
return -1;
if( pthread_create(&thread,&attr, &test_thread, NULL ) != 0 )
return -1;
if( pthread_attr_destroy(&attr) != 0 )
return -1;
/*
* while (1)
* {
* main thread logic...
* }
*/
Second, apply the detach option to an already created thread so that it returns its resources independently.
For reference, the first method is recommended. Changing a thread's attributes while it is running can confuse the logic, and although it is unlikely, the thread may already have finished before the detach option is applied.
Synchronization issues
Threads are terminated automatically when the main function exits. Therefore, for correct processing, main must wait until the threads have terminated before exiting. pthread_join is used for this.