[Return to Bookshelf] [Contents] [Previous Section] [Next Section] [Index] [Help]


pthread_create

Creates a thread.

Syntax

pthread_create(
               thread,
               attr,
               start_routine,
               arg );
 


Argument Data Type Access

thread opaque pthread_t write attr opaque pthread_attr_t read start_routine procedure read arg user_arg read


C Binding

#include 

int pthread_create ( pthread_t *thread, const pthread_attr_t *attr, void * (*start_routine) (void *), void *arg);

Arguments

thread
Thread object created.
attr
Thread attributes object that defines the characteristics of the thread being created. If you specify NULL, default attributes are used.
start_routine
Function executed as the new thread's start routine.
arg
Address value copied and passed to the thread's start routine.

Description

This routine creates a thread. A thread is a single, sequential flow of control within a program. It is the active execution of a designated routine, including any nested routine invocations.

Successful execution of this routine includes the following actions:

Thread Creation

A thread is created in the ready state to begin executing the function specified by the start_routine argument. The new thread may execute immediately. The newly created thread may preempt its creator, depending on scheduling policy and priority. The thread identifier (pthread_t) specified in pthread_create will be written before the new thread executes.

The new thread's scheduling policy and priority are, by default, inherited from the creating thread-the scheduling policy and priority set in the attributes object are ignored. To create a thread using the scheduling policy and priority set in the attributes object, you must first disable the inherit scheduling attribute by calling pthread_attr_setinheritsched before calling pthread_create.

The thread exists until it is both terminated and detached. A thread is detached when created if the detach state attribute of its thread object is set to PTHREAD_CREATE_DETACHED. It is also detached after any thread returns successfully from calling pthread_detach or pthread_join for the thread. Termination is explained in the next section (see Thread Termination).

The caller of pthread_create can synchronize with the newly created thread through the use of the pthread_join routine, or any other mutexes or condition variables they agree to use.

On Digital UNIX, the signal state of the new thread is initialized as follows:

  1. The signal mask is inherited from the creating thread.

  2. The set of signals pending for the new thread is empty.

If pthread_create fails, no new thread is created, and the contents of the location referenced by thread are undefined.

Thread Termination

A thread terminates when one of the following events occurs:

The following actions are performed when a thread terminates:

Return Values

If an error condition occurs, no thread is created, the contents of thread are undefined, and this routine returns an integer value indicating the type of error. Possible return values are as follows:
Return        Description

0 Successful completion. [EAGAIN] The system lacks the necessary resources to create another thread, or the system-imposed limit on the total number of threads under execution by a single user is exceeded. [EINVAL] The value specified by attr is invalid. [ENOMEM] Insufficient enough memory exists to create a thread. [EPERM] The caller does not have the appropriate permission to create a thread with the specified attributes.


Associated Routines

   pthread_atfork
   pthread_attr_destroy
   pthread_attr_init
   pthread_attr_setdetachstate
   pthread_attr_setinheritsched
   pthread_attr_setschedparam
   pthread_attr_setschedpolicy
   pthread_attr_setstacksize
   pthread_cancel
   pthread_detach
   pthread_exit
   pthread_join



[Return to Bookshelf] [Contents] [Previous Section] [Next Section] [Index] [Help]