Linux多线程学习(二)pthread_join
NAMEpthread_join - wait for thread termination 等待直到线程终止SYNOPSIS#include pthread.h>int pthread_join(pthread_t thread, void **value_ptr);DESCRIPTIONThe pthread_join() function suspe
NAME
pthread_join - wait for thread termination 等待直到线程终止
SYNOPSIS
#include <pthread.h> int pthread_join(pthread_t thread, void **value_ptr);
DESCRIPTION
The pthread_join() function suspends 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 is 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 will not be detached.It is unspecified whether a thread that has exited but remains unjoined counts against _POSIX_THREAD_THREADS_MAX.
RETURN VALUE
If successful, the pthread_join() function returns zero. Otherwise, an error number is returned to indicate the error.
ERRORS
The pthread_join() function will fail if:[EINVAL]
- The implementation has detected that the value specified by thread does not refer to a joinable thread.
[ESRCH]- No thread could be found corresponding to that specified by the given thread ID.
The pthread_join() function may fail if:
[EDEADLK]
- A deadlock was detected or the value of thread specifies the calling thread.
The pthread_join() function will not return an error code of [EINTR].
EXAMPLES
None.
APPLICATION USAGE
None.
FUTURE DIRECTIONS
None.
int pthread_join(pthread_t thread, void **value_ptr);
thread:等待退出线程的线程号。
value_ptr:退出线程的返回值。
范例:
//pthread_join.c
//
//
#include <stdio.h>
#include <unistd.h>
#include <signal.h>
#include <pthread.h>
#include <time.h>
pthread_t tid;
sigset_t set;
void myfunc()
{
}
static void* mythread(void *p)
{
}
int main()
{
}
编译 gcc -o pthread_join -lpthread pthread_join.c
运行结果:
// 如果输入a,子线程打印"hello",主程序继续等待输入;
// 如果输入q,主程序等待子程序结束。子线程打印"I will sleep 2 second and exit",并延时两秒后结束。主线程随之打印"finish",程序结束。
更多推荐
所有评论(0)