Movatterモバイル変換


[0]ホーム

URL:


<<< PreviousHomeNext>>>


NAME

pthread_create - thread creation

SYNOPSIS

#include <pthread.h>

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

DESCRIPTION

Thepthread_create() function shall create a new thread, with attributes specified byattr, within a process. Ifattr is NULL, the default attributes shall be used. If the attributes specified byattr are modified later, thethread's attributes shall not be affected. Upon successful completion,pthread_create() shall store the ID of the createdthread in the location referenced bythread.

The thread is created executingstart_routine witharg as its sole argument. If thestart_routine returns,the effect shall be as if there was an implicit call topthread_exit() usingthe return value ofstart_routine as the exit status. Note that the thread in whichmain() was originally invokeddiffers from this. When it returns frommain(), the effect shall be as if there was an implicit call toexit() using the return value ofmain() as the exit status.

The signal state of the new thread shall be initialized as follows:

The thread-local current locale[XSI][Option Start]  and the alternate stack[Option End] shall not be inherited.

The floating-point environment shall be inherited from the creating thread.

Ifpthread_create() fails, no new thread is created and the contents of the location referenced bythread areundefined.

[TCT][Option Start]If _POSIX_THREAD_CPUTIME is defined, the new thread shall have a CPU-time clock accessible, and the initial value of this clockshall be set to zero.[Option End]

The behavior is undefined if the value specified by theattr argument topthread_create() does not refer to aninitialized thread attributes object.

RETURN VALUE

If successful, thepthread_create() function shall return zero; otherwise, an error number shall be returned to indicatethe error.

ERRORS

Thepthread_create() function shall fail if:

[EAGAIN]
The system lacked the necessary resources to create another thread, or the system-imposed limit on the total number of threadsin a process {PTHREAD_THREADS_MAX} would be exceeded.
[EPERM]
The caller does not have appropriate privileges to set the required scheduling parameters or scheduling policy.

Thepthread_create() function shall not return an error code of [EINTR].


The following sections are informative.

EXAMPLES

None.

APPLICATION USAGE

There is no requirement on the implementation that the ID of the created thread be available before the newly created threadstarts executing. The calling thread can obtain the ID of the created thread through thethread argument of thepthread_create() function, and the newly created thread can obtain its ID by a call topthread_self().

RATIONALE

A suggested alternative topthread_create() would be to define two separate operations: create and start. Someapplications would find such behavior more natural. Ada, in particular, separates the "creation" of a task from its"activation".

Splitting the operation was rejected by the standard developers for many reasons:

An Ada implementor can choose to create the thread at either of two points in the Ada program: when the task object is created,or when the task is activated (generally at a "begin"). If the first approach is adopted, thestart_routine() needs towait on a condition variable to receive the order to begin "activation". The second approach requires no such condition variableor extra synchronization. In either approach, a separate Ada task control block would need to be created when the task object iscreated to hold rendezvous queues, and so on.

An extension of the preceding model would be to allow the state of the thread to be modified between the create and start. Thiswould allow the thread attributes object to be eliminated. This has been rejected because:

Another suggested alternative uses a model similar to that for process creation, such as "thread fork". The fork semanticswould provide more flexibility and the "create" function can be implemented simply by doing a thread fork followed immediately bya call to the desired "start routine" for the thread. This alternative has these problems:

If an implementation detects that the value specified by theattr argument topthread_create() does not refer toan initialized thread attributes object, it is recommended that the function should fail and report an [EINVAL] error.

FUTURE DIRECTIONS

None.

SEE ALSO

fork,pthread_exit,pthread_join

XBDMemory Synchronization,<pthread.h>

CHANGE HISTORY

First released in Issue 5. Included for alignment with the POSIX Threads Extension.

Issue 6

Thepthread_create() function is marked as part of the Threads option.

The following new requirements on POSIX implementations derive from alignment with the Single UNIX Specification:

The thread CPU-time clock semantics are added for alignment with IEEE Std 1003.1d-1999.

Therestrict keyword is added to thepthread_create() prototype for alignment with the ISO/IEC 9899:1999standard.

The DESCRIPTION is updated to make it explicit that the floating-point environment is inherited from the creating thread.

IEEE Std 1003.1-2001/Cor 1-2002, item XSH/TC1/D6/44 is applied, adding text that the alternate stack is notinherited.

IEEE Std 1003.1-2001/Cor 2-2004, item XSH/TC2/D6/93 is applied, updating the ERRORS section to remove themandatory [EINVAL] error (``The value specified byattr is invalid"), and adding the optional [EINVAL] error (``Theattributes specified byattr are invalid").

IEEE Std 1003.1-2001/Cor 2-2004, item XSH/TC2/D6/94 is applied, adding the APPLICATION USAGE section.

Issue 7

Thepthread_create() function is moved from the Threads option to the Base.

The [EINVAL] error for an uninitialized thread attributes object is removed; this condition results in undefined behavior.

POSIX.1-2008, Technical Corrigendum 1, XSH/TC1-2008/0458 [302] is applied.

POSIX.1-2008, Technical Corrigendum 2, XSH/TC2-2008/0274 [849] is applied.

End of informative text.

 

return to top of page


<<< PreviousHomeNext>>>


[8]ページ先頭

©2009-2026 Movatter.jp