
首先看一下两个函数的定义:
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.
线程1pthreadjoin返回值,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()时,无需等待子线程的完成,它可以马上执行下面的代码pthreadjoin返回值,当然,也有可能主线程执行完以后销毁进程,导致子线程无法执行,此外,一个PTHREAD_CREATE_DETACH状态的子线程拥有自我回收内存资源的功能。
本文来自电脑杂谈,转载请注明本文网址:
http://www.pc-fly.com/a/jisuanjixue/article-138027-1.html
稻谷收购价降了
大总裁咩
太帅