HP-UX Reference (11i v1 00/12) - 3 Library Functions N-Z (vol 7)
__________________________________________________________________________________________________________________________________________________________________________________________________
__________________________________________________________________________________________________________________________________________________________________________________________________
STANDARD Printed by: Nora Chuang [nchuang] STANDARD
/build/1111/BRICK/man3/nan.3m
________________________________________________________________
___ ___
p
pthread(3T) pthread(3T)
(Pthread Library)
NAME
pthread - Introduction To POSIX.1c Threads
DESCRIPTION
The POSIX.1c library developed by HP enables the creation of processes that can exploit application and
multi-processor platform parallelism. The pthread library libpthread consists of over 90 standardized inter-
faces for developing concurrent applications and synchronizing their actions within processes or between
them. This manual page presents an overview of libpthread including terminology and how to compile and
link programs which use threads.
COMPILATION SUMMARY
A multi-threaded application must define the appropriate POSIX revision level (199506) at compile time
and link againstthe pthread library via -lpthread. For example:
cc -D_POSIX_C_SOURCE=199506L -o myapp myapp.c -lpthread
All program sources must also include the header file <pthread.h>.
Note: When explicitly specifying ANSI compilation (via "-Aa"), defining the POSIX revision level restricts
the program to using interfaces within the POSIX namespaces. If interfaces in the larger X/Open
namespace are to be called, either of the compiler options,
-D_XOPEN_SOURCE_EXTENDED
or
-D_HPUX_SOURCE, must be specified in addition to
-D_POSIX_C_SOURCE=199506L. Alternatively,
compiling with -Ae (or not specifying "-A") will implicitly specify
-D_HPUX_SOURCE
.
Note: Some documentation will recommend the use of
-D_REENTRANT for compilation. While this also
functions properly, it is considered an obsolescent form.
THREAD OVERVIEW
A thread is an independent flow of control within a process, composed of a context (which includes a regis-
ter set and a program counter) and a sequence of instructions to execute.
All processes consist of at least one thread. Multi-threaded processes contain several threads. All threads
share the common address space allocated for the process. A program using the POSIX pthread APIs
creates and manipulates what are called user threads.Akernel thread is a kernel-schedulable entity which
may support one or more user threads. Currently, the HP-UX threads implementation supports only a
one-to-one mapping between user and kernel threads.
Each thread is assigned a unique identifier of type pthread_t upon creation. The thread id is a process-
private value and implementation-dependent. It is considered to be an opaque handle for the thread. Its
value should not be used by the application.
NOTES ON INTERFACES
The HP-UX system provides some non-standard extensions to the pthread API. These will always have a
distinguishing suffix of _np or _NP (non-portable).
The programmer should always consult the manpages for the functions being used. Some standard-
specified functions are not available or may have no effect in some implementations.
THREAD CREATION/DESTRUCTION
A program creates a thread using the
pthread_create()
function. When the thread has completed its
work, it may optionally call the
pthread_exit()
function, or simply return from its initial function. A
thread can detect the completion of another by using the
pthread_join()
function.
pthread_create() Creates a thread and assigns a unique identifier, pthread_t. The caller provides
a function which will be executed by the thread. Optionally, the call may expli-
citly specify some attributes for the thread (see PTHREAD ATTRIBUTES
below).
pthread_exit() Called by a thread when it completes. This function does not return.
pthread_join() This is analogous to wait(), but for pthreads. Any thread may join any other
thread in the process, there is no parent/child relationship. It returns when a
specified thread terminates, and the thread resources have been reaped.
pthread_detach() Makes it unnecessary to "join" the thread. Thread resources are reaped by the
system at the time the thread terminates.
HP-UX Release 11i: December 2000 − 1 − Section 3−−631
___
___