[ Bottom of Page | Previous Page | Next Page | Contents | Index | Library Home | Legal | Search ]

Technical Reference: Base Operating System and Extensions, Volume 1

pthread_create Subroutine

Purpose

Creates a new thread, initializes its attributes, and makes it runnable.

Library

Threads Library (libpthreads.a)

Syntax

#include <pthread.h>

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

Description

The pthread_create subroutine creates a new thread and initializes its attributes using the thread attributes object specified by the attr parameter. The new thread inherits its creating thread's signal mask; but any pending signal of the creating thread will be cleared for the new thread.

The new thread is made runnable, and will start executing the start_routine routine, with the parameter specified by the arg parameter. The arg parameter is a void pointer; it can reference any kind of data. It is not recommended to cast this pointer into a scalar data type (int for example), because the casts may not be portable.

After thread creation, the thread attributes object can be reused to create another thread, or deleted.

The thread terminates in the following cases:

When multiple threads are created in a process, the FULL_CORE flag is set for all signals. This means that if a core file is produced, it will be much bigger than a single_threaded application. This is necessary to debug multiple-threaded processes.

When a process uses the pthread_create function, and thus becomes multi-threaded, the FULL_CORE flag is enabled for all signals. If a signal is received whose action is to terminate the process with a core dump, a full dump (usually much larger than a regular dump) will be produced. This is necessary so that multi-threaded programs can be debugged with the dbx command.

The following piece of pseudocode is an example of how to avoid getting a full core. Please note that in this case, debug will not be possible. It may be easier to limit the size of the core with the ulimit command.

struct sigaction siga;
siga.sa_handler = SIG_DFL;
siga.sa_flags = SA_RESTART;
SIGINITSET(siga.as_mask);
sigaction(<SIGNAL_NUMBER>, &siga, NULL);

The alternate stack is not inherited.

Parameters

thread Points to where the thread ID will be stored.
attr Specifies the thread attributes object to use in creating the thread. If the value is NULL, the default attributes values will be used.
start_routine Points to the routine to be executed by the thread.
arg Points to the single argument to be passed to the start_routine routine.

Return Values

If successful, the pthread_create function returns zero. Otherwise, an error number is returned to indicate the error.

Error Codes

The pthread_create function will fail if:

EAGAIN If WLM is running, the limit on the number of threads in the class may have been met.
EINVAL The value specified by attr is invalid.
EPERM The caller does not have appropriate permission to set the required scheduling parameters or scheduling policy.

The pthread_create function will not return an error code of EINTR.

Related Information

The core file format.

The dbx and ulimit commands.

The pthread_attr_init (pthread_attr_init Subroutine) subroutine, pthread_attr_destroy (pthread_attr_destroy Subroutine) subroutine, pthread_exit (pthread_exit Subroutine) subroutine, pthread_cancel (pthread_cancel Subroutine) subroutine, pthread_kill (pthread_kill Subroutine) subroutine, pthread_self (pthread_self Subroutine) subroutine, pthread_once (pthread_once Subroutine) subroutine, pthread_join (pthread_join or pthread_detach Subroutine) subroutine, fork (fork, f_fork, or vfork Subroutine) subroutine, and the pthread.h file.

Creating Threads in AIX 5L Version 5.2 General Programming Concepts: Writing and Debugging Programs.

Threads Library Quick Reference in AIX 5L Version 5.2 General Programming Concepts: Writing and Debugging Programs.

[ Top of Page | Previous Page | Next Page | Contents | Index | Library Home | Legal | Search ]