Java Tutorial
Java Tutorial
NAME
SYNOPSIS
[Option End]
DESCRIPTION
The pthread_join() function shall suspend execution of the calling thread until the target thread
terminates, unless the target thread has already terminated. On return from a successful
pthread_join() call with a non-NULL value_ptr argument, the value passed to pthread_exit() by
the terminating thread shall be made available in the location referenced by value_ptr. When a
pthread_join() returns successfully, the target thread has been terminated. The results of
multiple simultaneous calls to pthread_join() specifying the same target thread are undefined. If
the thread calling pthread_join() is canceled, then the target thread shall not be detached.
It is unspecified whether a thread that has exited but remains unjoined counts against
{PTHREAD_THREADS_MAX}.
RETURN VALUE
If successful, the pthread_join() function shall return zero; otherwise, an error number shall be
returned to indicate the error.
ERRORS
No thread could be found corresponding to that specified by the given thread ID.
[EDEADLK]
A deadlock was detected or the value of thread specifies the calling thread.
[EINVAL]
EXAMPLES
typedef struct {
int *ar;
long n;
} subarray;
int main(void)
{
int ar[1000000];
pthread_t th1, th2;
subarray sb1, sb2;
sb1.ar = &ar[0];
sb1.n = 500000;
(void) pthread_create(&th1, NULL, incer, &sb1);
sb2.ar = &ar[500000];
sb2.n = 500000;
(void) pthread_create(&th2, NULL, incer, &sb2);
(void) pthread_join(th1, NULL);
(void) pthread_join(th2, NULL);
return 0;
}
APPLICATION USAGE
None.
RATIONALE
The pthread_join() function provides a simple mechanism allowing an application to wait for a
thread to terminate. After the thread terminates, the application may then choose to clean up
resources that were used by the thread. For instance, after pthread_join() returns, any
application-provided stack storage could be reclaimed.
The pthread_join() or pthread_detach() function should eventually be called for every thread
that is created with the detachstate attribute set to PTHREAD_CREATE_JOINABLE so that storage
associated with the thread may be reclaimed.
The interaction between pthread_join() and cancellation is well-defined for the following
reasons:
The pthread_join() function, like all other non-async-cancel-safe functions, can only be called
with deferred cancelability type.
Thus, only the default cancelability state need be considered. As specified, either the
pthread_join() call is canceled, or it succeeds, but not both. The difference is obvious to the
application, since either a cancellation handler is run or pthread_join() returns. There are no race
conditions since pthread_join() was called in the deferred cancelability state.
FUTURE DIRECTIONS
None.
SEE ALSO
pthread_create(), wait(), the Base Definitions volume of IEEE Std 1003.1-2001, <pthread.h>
CHANGE HISTORY
First released in Issue 5. Included for alignment with the POSIX Threads Extension.
Issue 6
IEEE Std 1003.1-2001/Cor 2-2004, XSH/TC2/D6/97 is applied, updating the ERRORS section so
that the [EINVAL] error is made optional and the words ``the implementation has detected'' are
removed from it.
//
NAME
SYNOPSIS
#include <pthread.h>
The mutex object referenced by mutex shall be locked by calling pthread_mutex_lock(). If the
mutex is already locked, the calling thread shall block until the mutex becomes available. This
operation shall return with the mutex object referenced by mutex in the locked state with the
calling thread as its owner.
DESCRIPTION
The mutex object referenced by mutex is locked by calling pthread_mutex_lock(). If the mutex is
already locked, the calling thread blocks until the mutex becomes available. This operation
returns with the mutex object referenced by mutex in the locked state with the calling thread as
its owner.
If the mutex type is PTHREAD_MUTEX_RECURSIVE, then the mutex maintains the concept of a
lock count. When a thread successfully acquires a mutex for the first time, the lock count is set
to one. Every time a thread relocks this mutex, the lock count is incremented by one. Each time
the thread unlocks the mutex, the lock count is decremented by one. When the lock count
reaches zero, the mutex becomes available for other threads to acquire. If a thread attempts to
unlock a mutex that it has not locked or a mutex which is unlocked, an error will be returned.
If the mutex type is PTHREAD_MUTEX_DEFAULT, attempting to recursively lock the mutex results
in undefined behaviour. Attempting to unlock the mutex if it was not locked by the calling thread
results in undefined behaviour. Attempting to unlock the mutex if it is not locked results in
undefined behaviour.
The pthread_mutex_unlock() function releases the mutex object referenced by mutex. The
manner in which a mutex is released is dependent upon the mutex's type attribute. If there are
threads blocked on the mutex object referenced by mutex when pthread_mutex_unlock() is
called, resulting in the mutex becoming available, the scheduling policy is used to determine
which thread shall acquire the mutex. (In the case of PTHREAD_MUTEX_RECURSIVE mutexes, the
mutex becomes available when the count reaches zero and the calling thread no longer has any
locks on this mutex).
If a signal is delivered to a thread waiting for a mutex, upon return from the signal handler the
thread resumes waiting for the mutex as if it was not interrupted.
RETURN VALUE
The function pthread_mutex_trylock() returns zero if a lock on the mutex object referenced by
mutex is acquired. Otherwise, an error number is returned to indicate the error.
ERRORS
The mutex was created with the protocol attribute having the value PTHREAD_PRIO_PROTECT
and the calling thread's priority is higher than the mutex's current priority ceiling.
[EBUSY]
[EINVAL]
The value specified by mutex does not refer to an initialised mutex object.
[EAGAIN]
The mutex could not be acquired because the maximum number of recursive locks for mutex has
been exceeded.
[EDEADLK]
[EPERM]
EXAMPLES
None.
APPLICATION USAGE
None.
FUTURE DIRECTIONS
None.
SEE ALSO
DERIVATION
//
NAME
SYNOPSIS
#include <pthread.h>
DESCRIPTION
The pthread_mutex_init() function initialises the mutex referenced by mutex with attributes
specified by attr. If attr is NULL, the default mutex attributes are used; the effect is the same as
passing the address of a default mutex attributes object. Upon successful initialisation, the state
of the mutex becomes initialised and unlocked.
It is safe to destroy an initialised mutex that is unlocked. Attempting to destroy a locked mutex
results in undefined behaviour.
RETURN VALUE
ERRORS
[EAGAIN]
The system lacked the necessary resources (other than memory) to initialise another mutex.
[ENOMEM]
[EPERM]
The caller does not have the privilege to perform the operation.
The implementation has detected an attempt to re-initialise the object referenced by mutex, a
previously initialised, but not yet destroyed, mutex.
[EINVAL]
[EBUSY]
The implementation has detected an attempt to destroy the object referenced by mutex while it
is locked or referenced (for example, while being used in a pthread_cond_wait() or
pthread_cond_timedwait()) by another thread.
[EINVAL]
EXAMPLES
None.
APPLICATION USAGE
None.
FUTURE DIRECTIONS
None.
SEE ALSO
DERIVATION