pthread.3t (2010 09)

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
multiprocessor platform parallelism. The pthread library libpthread consists of over 90 standardized
interfaces 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 multithreaded application must define the appropriate POSIX revision level (199506) at compile time
and link against the pthread library with
-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: If
-lc is explicitly specified in the link line, then it must be after the
-lpthread. Refer to
pthread_stubs(5) for more details.
Note: When explicitly specifying ANSI compilation (with "-Aa"), defining the POSIX revision level res-
tricts 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. Although 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
register 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. At HP-UX release 11i Version 1.6 and forward, the
HP-UX threads implementation supports many-to-any as well as 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 pro-
vides a function which will be executed by the thread. Optionally, the call
may explicitly specify some attributes for the thread (see PTHREAD ATTRI-
BUTES 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.
HP-UX 11i Version 3: September 2010 1 Hewlett-Packard Company 1

Summary of content (12 pages)