Name

pthread_join
- wait for thread termination

Library

libc_r.lib libpthread.lib libthr.lib

Synopsis

  #include <pthread.h>
  int pthread_join (pthread_t thrHandle, void **retValPtr);

Return values

If successful, the pthread_join function will return zero. Otherwise an error number will be returned to indicate the error.

Detailed description

The pthread_join function suspends execution of the calling thread until the target thrHandle terminates unless the target thrHandle has already terminated.

On return from a successful pthread_join call with a non-NULL retValPtr argument, the value passed to pthread_exit by the terminating thread is stored in the location referenced by retValPtr. 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.


Examples

/* Thread’s function. */
void *a_thread_func_1_1(void*)
{
 int i;
 printf("Wait for 3 seconds for thread to finish execution:0);
 for(i=1;i<4;i++)
 {
  printf("Waited (%d) second0, i);
  sleep(1);
 }
 return NULL;
}
int XYZ()
{
 pthread_t new_th;
 /* Create a new thread. */
 if(pthread_create(&new_th, NULL, a_thread_func_1_1, NULL) != 0)
 {
  perror("Error creating thread\n");
  return -1;
 }
 /* Wait for thread to return */
 if(pthread_join(new_th, NULL) != 0)
 {
  perror("Error in pthread_join()\n");
  return -1;
 }

         

Errors

The pthread_join function will fail if:
[EINVAL]
  The implementation has detected that the value specified by thrHandle does not refer to a joinable thread. or some other thread has already joined
[ESRCH]
  No thread could be found corresponding to that specified by the given thread ID, thrHandle.
[EDEADLK]
  A deadlock was detected or the value of thrHandle specifies the calling thread.

See also

wait, pthread_create


Feedback

For additional information or queries on this page send feedback

© 2005-2007 Nokia

Top