您的位置:首页 > 其它

简析pthread_join()和pthread_detach()

2013-07-12 20:50 357 查看
/article/9901728.html

首先看一下两个函数的定义:

int pthread_join(

pthread_t tid ,

void **status

);

// 参数tid 是希望等待的线程的线程号,status 是指向线程返回值的指针,线程的返回值就是pthread_exit 中的value_ptr 参数,或者是return语句中的返回值。该函数可用于线程间的同步

int pthread_detach( pthread_t pid );

//参数tid 是希望等待的线程的线程号, 把指定的线程转变为脱离状态

一个线程或者是可汇合的(joinable,缺省值),或者是脱离的(detached)。当一个可汇合的线程终止时,它的线程ID和退出状态将留到另一个线程对它调用pthread_join。脱离线程却象守护进程:当它们终止的时,所有相关资源都被释放,我们不能等待它们终止。如果一个线程需要知道另一个线程什么时候终止,那就最好好吃第二个线程的可汇合状态。

下面通过例子说明:

#include <stdio.h>

#include <stdlib.h>

#include <unistd.h>

#include <pthread.h>

#define THREAD_NUMBER 2

int retval_hello1= 1, retval_hello2 = 2;

void* hello1(void *arg)

{

char *hello_str = (char *)arg;

sleep(2);

printf("%s/n", hello_str);

pthread_exit(&retval_hello1);

}

void* hello2(void *arg)

{

char *hello_str = (char *)arg;

sleep(1);

printf("%s/n", hello_str);

pthread_exit(&retval_hello2);

}

int main(int argc, char *argv[])

{

int i;

int ret_val;

int *retval_hello[2];

pthread_t pt[THREAD_NUMBER];

const char *arg[THREAD_NUMBER];

arg[0] = "hello world from thread1";

arg[1] = "hello world from thread2";

printf("Begin to create threads.../n");

ret_val = pthread_create(&pt[0], NULL, hello1, (void *)arg[0]);

if (ret_val != 0 ) {

printf("pthread_create error!/n");

exit(1);

}

ret_val = pthread_create(&pt[1], NULL, hello2, (void *)arg[1]);

if (ret_val != 0 ) {

printf("pthread_create error!/n");

exit(1);

}

printf("Begin to wait for threads.../n");

for(i = 0; i < THREAD_NUMBER; i++) {

ret_val = pthread_join(pt[i], (void **)&retval_hello[i]);

if (ret_val != 0) {

printf("pthread_join error!/n");

exit(1);

} else {

printf("return value is %d/n", *retval_hello[i]);

}

}

printf("Now, the main thread returns./n");

return 0;

}

执行结果为:

Begin to create threads...

Begin to wait for threads...

hello world from thread2

hello world from thread1

return value is 1

return value is 2

Now, the main thread returns.


线程1,2的执行顺序可以通过sleep来调节,但是主线程必须在子线程完成之后才能执行,即打印”Now, the main thread returns.“。此外,因为调用pthread_join()的顺序,必定是线程1先执行“return value is xx”,不管线程2是否先执行完。

下面修改pthread_join为pthread_detach(),代码为

#include <stdio.h>

#include <stdlib.h>

#include <unistd.h>

#include <pthread.h>

#define THREAD_NUMBER 2

int retval_hello1= 1, retval_hello2 = 2;

void* hello1(void *arg)

{

char *hello_str = (char *)arg;

sleep(2);

printf("%s/n", hello_str);

pthread_exit(&retval_hello1);

}

void* hello2(void *arg)

{

char *hello_str = (char *)arg;

sleep(1);

printf("%s/n", hello_str);

pthread_exit(&retval_hello2);

}

int main(int argc, char *argv[])

{

int i;

int ret_val;

int *retval_hello[2];

pthread_t pt[THREAD_NUMBER];

const char *arg[THREAD_NUMBER];

arg[0] = "hello world from thread1";

arg[1] = "hello world from thread2";

printf("Begin to create threads.../n");

ret_val = pthread_create(&pt[0], NULL, hello1, (void *)arg[0]);

if (ret_val != 0 ) {

printf("pthread_create error!/n");

exit(1);

}

ret_val = pthread_create(&pt[1], NULL, hello2, (void *)arg[1]);

if (ret_val != 0 ) {

printf("pthread_create error!/n");

exit(1);

}

printf("Begin to wait for threads.../n");

for(i = 0; i < THREAD_NUMBER; i++) {

ret_val = pthread_detach(pt[i]);

if (ret_val != 0) {

printf("pthread_join error!/n");

exit(1);

}

}

printf("Now, the main thread returns./n");

return 0;

}

执行结果为

Begin to create threads...

Begin to wait for threads...

Now, the main thread returns.


线程1,2没有执行(也可能执行),因为子线程为可分离的,主线程在执行完之后即将进程销毁,资源收回,导致子线程未运行。可以在return 0 语句之前加入sleep(5),这样执行结果为

Begin to create threads...

Begin to wait for threads...

Now, the main thread returns.

hello world from thread2

hello world from thread1


所以,pthread_join()会挂起父线程,直至子线程完成才可以执行后面的代码,此外,一个PTHREAD_CREATE_JOINABLE状态的子线程不会自动释放该线程的内存资源,包括线程描述符和其使用的栈;而主线程调用pthread_detach()时,无需等待子线程的完成,它可以立即执行后面的代码,当然,也有可能主线程执行完之后销毁进程,导致子线程未能执行,此外,一个PTHREAD_CREATE_DETACH状态的子线程拥有自我回收内存资源的功能。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: