#include <stdio.h>
#include <pthread.h>
pthread_t tid;
void *func(void *args) {
printf("Thread running...");
pthread_join(tid, NULL);
printf("Thread waiting...");
return NULL;
}
int main(void) {
printf("Hello World\n");
pthread_create(&tid, NULL, func, NULL);
pthread_join(tid, NULL);
return 0;
}
pthread_join:
函数pthread_join()函数挂起调用线程的执行,直到目标线程之三结束为止,除非目标线程已经终止。
但是上述代码实际并不会死锁。
原因:
pthread_join(3) - Linux manual page
实际上这种情况pthread_join会直接返回EDEADLK,并不会一直阻塞!!!
RETURN VALUE top
On success, pthread_join() returns 0; on error, it returns an error number.
ERRORS top
EDEADLK A deadlock was detected (e.g., two threads tried to join with each other); or thread specifies the calling thread. EINVAL thread is not a joinable thread. EINVAL Another thread is already waiting to join with this thread. ESRCH No thread with the ID thread could be found.