Blame man-pages-posix-2013-a/man3p/pthread_create.3p

Packit 7cfc04
'\" et
Packit 7cfc04
.TH PTHREAD_CREATE "3P" 2013 "IEEE/The Open Group" "POSIX Programmer's Manual"
Packit 7cfc04
.SH PROLOG
Packit 7cfc04
This manual page is part of the POSIX Programmer's Manual.
Packit 7cfc04
The Linux implementation of this interface may differ (consult
Packit 7cfc04
the corresponding Linux manual page for details of Linux behavior),
Packit 7cfc04
or the interface may not be implemented on Linux.
Packit 7cfc04
Packit 7cfc04
.SH NAME
Packit 7cfc04
pthread_create
Packit 7cfc04
\(em thread creation
Packit 7cfc04
.SH SYNOPSIS
Packit 7cfc04
.LP
Packit 7cfc04
.nf
Packit 7cfc04
#include <pthread.h>
Packit 7cfc04
.P
Packit 7cfc04
int pthread_create(pthread_t *restrict \fIthread\fP,
Packit 7cfc04
    const pthread_attr_t *restrict \fIattr\fP,
Packit 7cfc04
    void *(*\fIstart_routine\fP)(void*), void *restrict \fIarg\fP);
Packit 7cfc04
.fi
Packit 7cfc04
.SH DESCRIPTION
Packit 7cfc04
The
Packit 7cfc04
\fIpthread_create\fR()
Packit 7cfc04
function shall create a new thread, with attributes specified by
Packit 7cfc04
.IR attr ,
Packit 7cfc04
within a process. If
Packit 7cfc04
.IR attr
Packit 7cfc04
is NULL, the default attributes shall be used. If the attributes
Packit 7cfc04
specified by
Packit 7cfc04
.IR attr
Packit 7cfc04
are modified later, the thread's attributes shall not be affected.
Packit 7cfc04
Upon successful completion,
Packit 7cfc04
\fIpthread_create\fR()
Packit 7cfc04
shall store the ID of the created thread in the location referenced by
Packit 7cfc04
.IR thread .
Packit 7cfc04
.P
Packit 7cfc04
The thread is created executing
Packit 7cfc04
.IR start_routine
Packit 7cfc04
with
Packit 7cfc04
.IR arg
Packit 7cfc04
as its sole argument. If the
Packit 7cfc04
.IR start_routine
Packit 7cfc04
returns, the effect shall be as if there was an implicit call to
Packit 7cfc04
\fIpthread_exit\fR()
Packit 7cfc04
using the return value of
Packit 7cfc04
.IR start_routine
Packit 7cfc04
as the exit status. Note that the thread in which
Packit 7cfc04
\fImain\fR()
Packit 7cfc04
was originally invoked differs from this. When it returns from
Packit 7cfc04
\fImain\fR(),
Packit 7cfc04
the effect shall be as if there was an implicit call to
Packit 7cfc04
\fIexit\fR()
Packit 7cfc04
using the return value of
Packit 7cfc04
\fImain\fR()
Packit 7cfc04
as the exit status.
Packit 7cfc04
.P
Packit 7cfc04
The signal state of the new thread shall be initialized as follows:
Packit 7cfc04
.IP " *" 4
Packit 7cfc04
The signal mask shall be inherited from the creating thread.
Packit 7cfc04
.IP " *" 4
Packit 7cfc04
The set of signals pending for the new thread shall be empty.
Packit 7cfc04
.P
Packit 7cfc04
The thread-local current locale
Packit 7cfc04
and the alternate stack
Packit 7cfc04
shall not be inherited.
Packit 7cfc04
.P
Packit 7cfc04
The floating-point environment shall be inherited from the creating
Packit 7cfc04
thread.
Packit 7cfc04
.P
Packit 7cfc04
If
Packit 7cfc04
\fIpthread_create\fR()
Packit 7cfc04
fails, no new thread is created and the contents of the location
Packit 7cfc04
referenced by
Packit 7cfc04
.IR thread
Packit 7cfc04
are undefined.
Packit 7cfc04
.P
Packit 7cfc04
If _POSIX_THREAD_CPUTIME is defined, the new thread shall have a
Packit 7cfc04
CPU-time clock accessible, and the initial value of this clock shall
Packit 7cfc04
be set to zero.
Packit 7cfc04
.P
Packit 7cfc04
The behavior is undefined if the value specified by the
Packit 7cfc04
.IR attr
Packit 7cfc04
argument to
Packit 7cfc04
\fIpthread_create\fR()
Packit 7cfc04
does not refer to an initialized thread attributes object.
Packit 7cfc04
.SH "RETURN VALUE"
Packit 7cfc04
If successful, the
Packit 7cfc04
\fIpthread_create\fR()
Packit 7cfc04
function shall return zero; otherwise, an error number shall be
Packit 7cfc04
returned to indicate the error.
Packit 7cfc04
.SH ERRORS
Packit 7cfc04
The
Packit 7cfc04
\fIpthread_create\fR()
Packit 7cfc04
function shall fail if:
Packit 7cfc04
.TP
Packit 7cfc04
.BR EAGAIN
Packit 7cfc04
The system lacked the necessary resources to create another thread, or
Packit 7cfc04
the system-imposed limit on the total number of threads in a process
Packit 7cfc04
{PTHREAD_THREADS_MAX}
Packit 7cfc04
would be exceeded.
Packit 7cfc04
.TP
Packit 7cfc04
.BR EPERM
Packit 7cfc04
The caller does not have appropriate privileges to set the required
Packit 7cfc04
scheduling parameters or scheduling policy.
Packit 7cfc04
.P
Packit 7cfc04
The
Packit 7cfc04
\fIpthread_create\fR()
Packit 7cfc04
function shall not return an error code of
Packit 7cfc04
.BR [EINTR] .
Packit 7cfc04
.LP
Packit 7cfc04
.IR "The following sections are informative."
Packit 7cfc04
.SH EXAMPLES
Packit 7cfc04
None.
Packit 7cfc04
.SH "APPLICATION USAGE"
Packit 7cfc04
There is no requirement on the implementation that the ID of the
Packit 7cfc04
created thread be available before the newly created thread starts
Packit 7cfc04
executing. The calling thread can obtain the ID of the created thread
Packit 7cfc04
through the return value of the
Packit 7cfc04
\fIpthread_create\fR()
Packit 7cfc04
function, and the newly created thread can obtain its ID by a call to
Packit 7cfc04
\fIpthread_self\fR().
Packit 7cfc04
.SH RATIONALE
Packit 7cfc04
A suggested alternative to
Packit 7cfc04
\fIpthread_create\fR()
Packit 7cfc04
would be to define two separate operations: create and start. Some
Packit 7cfc04
applications would find such behavior more natural. Ada, in
Packit 7cfc04
particular, separates the ``creation'' of a task from its
Packit 7cfc04
``activation''.
Packit 7cfc04
.P
Packit 7cfc04
Splitting the operation was rejected by the standard developers for
Packit 7cfc04
many reasons:
Packit 7cfc04
.IP " *" 4
Packit 7cfc04
The number of calls required to start a thread would increase from one
Packit 7cfc04
to two and thus place an additional burden on applications that do not
Packit 7cfc04
require the additional synchronization. The second call, however,
Packit 7cfc04
could be avoided by the additional complication of a start-up state
Packit 7cfc04
attribute.
Packit 7cfc04
.IP " *" 4
Packit 7cfc04
An extra state would be introduced: ``created but not started''. This
Packit 7cfc04
would require the standard to specify the behavior of the thread
Packit 7cfc04
operations when the target has not yet started executing.
Packit 7cfc04
.IP " *" 4
Packit 7cfc04
For those applications that require such behavior, it is possible to
Packit 7cfc04
simulate the two separate steps with the facilities that are currently
Packit 7cfc04
provided. The
Packit 7cfc04
\fIstart_routine\fR()
Packit 7cfc04
can synchronize by waiting on a condition variable that is signaled by
Packit 7cfc04
the start operation.
Packit 7cfc04
.P
Packit 7cfc04
An Ada implementor can choose to create the thread at either of two
Packit 7cfc04
points in the Ada program: when the task object is created, or when
Packit 7cfc04
the task is activated (generally at a ``begin''). If the first
Packit 7cfc04
approach is adopted, the
Packit 7cfc04
\fIstart_routine\fR()
Packit 7cfc04
needs to wait on a condition variable to receive the order to begin
Packit 7cfc04
``activation''. The second approach requires no such condition
Packit 7cfc04
variable or extra synchronization. In either approach, a separate Ada
Packit 7cfc04
task control block would need to be created when the task object is
Packit 7cfc04
created to hold rendezvous queues, and so on.
Packit 7cfc04
.P
Packit 7cfc04
An extension of the preceding model would be to allow the state of the
Packit 7cfc04
thread to be modified between the create and start. This would allow
Packit 7cfc04
the thread attributes object to be eliminated. This has been rejected
Packit 7cfc04
because:
Packit 7cfc04
.IP " *" 4
Packit 7cfc04
All state in the thread attributes object has to be able to be set for
Packit 7cfc04
the thread. This would require the definition of functions to modify
Packit 7cfc04
thread attributes. There would be no reduction in the number of
Packit 7cfc04
function calls required to set up the thread. In fact, for an
Packit 7cfc04
application that creates all threads using identical attributes, the
Packit 7cfc04
number of function calls required to set up the threads would be
Packit 7cfc04
dramatically increased. Use of a thread attributes object permits the
Packit 7cfc04
application to make one set of attribute setting function calls.
Packit 7cfc04
Otherwise, the set of attribute setting function calls needs to be made
Packit 7cfc04
for each thread creation.
Packit 7cfc04
.IP " *" 4
Packit 7cfc04
Depending on the implementation architecture, functions to set thread
Packit 7cfc04
state would require kernel calls, or for other implementation reasons
Packit 7cfc04
would not be able to be implemented as macros, thereby increasing the
Packit 7cfc04
cost of thread creation.
Packit 7cfc04
.IP " *" 4
Packit 7cfc04
The ability for applications to segregate threads by class would be
Packit 7cfc04
lost.
Packit 7cfc04
.P
Packit 7cfc04
Another suggested alternative uses a model similar to that for process
Packit 7cfc04
creation, such as ``thread fork''. The fork semantics would provide
Packit 7cfc04
more flexibility and the ``create'' function can be implemented simply
Packit 7cfc04
by doing a thread fork followed immediately by a call to the desired
Packit 7cfc04
``start routine'' for the thread. This alternative has these
Packit 7cfc04
problems:
Packit 7cfc04
.IP " *" 4
Packit 7cfc04
For many implementations, the entire stack of the calling thread would
Packit 7cfc04
need to be duplicated, since in many architectures there is no way to
Packit 7cfc04
determine the size of the calling frame.
Packit 7cfc04
.IP " *" 4
Packit 7cfc04
Efficiency is reduced since at least some part of the stack has to be
Packit 7cfc04
copied, even though in most cases the thread never needs the copied
Packit 7cfc04
context, since it merely calls the desired start routine.
Packit 7cfc04
.P
Packit 7cfc04
If an implementation detects that the value specified by the
Packit 7cfc04
.IR attr
Packit 7cfc04
argument to
Packit 7cfc04
\fIpthread_create\fR()
Packit 7cfc04
does not refer to an initialized thread attributes object, it is
Packit 7cfc04
recommended that the function should fail and report an
Packit 7cfc04
.BR [EINVAL] 
Packit 7cfc04
error.
Packit 7cfc04
.SH "FUTURE DIRECTIONS"
Packit 7cfc04
None.
Packit 7cfc04
.SH "SEE ALSO"
Packit 7cfc04
.IR "\fIfork\fR\^(\|)",
Packit 7cfc04
.IR "\fIpthread_exit\fR\^(\|)",
Packit 7cfc04
.IR "\fIpthread_join\fR\^(\|)"
Packit 7cfc04
.P
Packit 7cfc04
The Base Definitions volume of POSIX.1\(hy2008,
Packit 7cfc04
.IR "Section 4.11" ", " "Memory Synchronization",
Packit 7cfc04
.IR "\fB<pthread.h>\fP"
Packit 7cfc04
.SH COPYRIGHT
Packit 7cfc04
Portions of this text are reprinted and reproduced in electronic form
Packit 7cfc04
from IEEE Std 1003.1, 2013 Edition, Standard for Information Technology
Packit 7cfc04
-- Portable Operating System Interface (POSIX), The Open Group Base
Packit 7cfc04
Specifications Issue 7, Copyright (C) 2013 by the Institute of
Packit 7cfc04
Electrical and Electronics Engineers, Inc and The Open Group.
Packit 7cfc04
(This is POSIX.1-2008 with the 2013 Technical Corrigendum 1 applied.) In the
Packit 7cfc04
event of any discrepancy between this version and the original IEEE and
Packit 7cfc04
The Open Group Standard, the original IEEE and The Open Group Standard
Packit 7cfc04
is the referee document. The original Standard can be obtained online at
Packit 7cfc04
http://www.unix.org/online.html .
Packit 7cfc04
Packit 7cfc04
Any typographical or formatting errors that appear
Packit 7cfc04
in this page are most likely
Packit 7cfc04
to have been introduced during the conversion of the source files to
Packit 7cfc04
man page format. To report such errors, see
Packit 7cfc04
https://www.kernel.org/doc/man-pages/reporting_bugs.html .