POSIX Threads (Pthreads)
An Introduction |
Prof. David Bernstein
|
Computer Science Department |
bernstdh@jmu.edu |
errno
and Condition Codesint pthread_create(pthread_t *thread, const pthread_attr_t *attr, void *(*start)(void *), void *arg)
thread
|
Identifier of the new thread |
start
|
The function to be executed in the new thread |
attr
|
Attributes of the new thread (or NULL to use the default attributes) |
arg
|
The argument to pass to start() |
Return | 0 on success; a positive error number on error |
Note:The new thread may start executing before pthread_create()
returns.
start()
function returnsexit()
main()
pthread_exit()
int pthread_detach(pthread_t thread)
thread
|
The thread to detach |
Return | 0 on success; a positive error number of error |
Note: Detaching a thread does not terminate it nor does it change the way in which a thread can/will be terminated.
int pthread_join(pthread_t thread, void **retval)
thread
|
The thread to wait for |
retval
|
A non-NULL pointer that receives a copy of the terminated thread's return value |
Return | 0 on success; a positive error number on error |
Notes: (1) If thread
has already terminated
then pthread_join()
will return immediately. (2) One must not call
pthread_join()
on a thread that has already been joined
(since the ID may have been re-used).
-pthread
-pthread
int pthread_once(pthread_once_t *control, void (*init)(void))
control
|
Pointer to a statically initialized variable with the value PTHREAD_ONCE_INIT |
init
|
Pointer to the function that is to be executed once |
Return | 0 on success; positive error number on error |
The first call to pthread_once()
modifies the value of the variable
pointed to by control
and then calls init()
.
Subsequent calls to pthread_once()
then don't call
init()
.