Multithreaded Programming Guide

Creating a Default Thread

When an attribute object is not specified, the object is NULL, and the default thread is created with the following attributes:

You can also create a default attribute object with pthread_attr_init() , and then use this attribute object to create a default thread. See the section Initializing Attributes for details.

pthread_create Syntax

Use pthread_create(3C) to add a new thread of control to the current process.

int pthread_create(pthread_t *restrict tid, const pthread_attr_t 
    *restrict tattr, void*(*start_routine)(void *), void *restrict arg);
#include <pthread.h>

pthread_attr_t() tattr;
pthread_t tid;
extern void *start_routine(void *arg);
void *arg;
int ret; 

/* default behavior*/
ret = pthread_create(&tid, NULL, start_routine, arg);

/* initialized with default attributes */
ret = pthread_attr_init(&tattr);
/* default behavior specified*/
ret = pthread_create(&tid, &tattr, start_routine, arg);

The pthread_create() function is called with attr that has the necessary state behavior. start_routine is the function with which the new thread begins execution. When start_routine returns, the thread exits with the exit status set to the value returned by start_routine. See pthread_create Syntax.

When pthread_create() is successful, the ID of the created thread is stored in the location referred to as tid.

When you call pthread_create() with either a NULL attribute argument or a default attribute, pthread_create() creates a default thread. When tattr is initialized, the thread acquires the default behavior.

pthread_create Return Values

pthread_create() returns zero when the call completes successfully. Any other return value indicates that an error occurred. When any of the following conditions are detected, pthread_create() fails and returns the corresponding value.



A system limit is exceeded, such as when too many threads have been created.



The value of tattr is invalid.



The caller does not have appropriate permission to set the required scheduling parameters or scheduling policy.