是用pthread_exit函数时注意的问题

来源:互联网 发布:淘宝卖家评分怎么算的 编辑:程序博客网 时间:2024/05/17 00:04

是用pthread_exit函数时注意的问题

今天看到这个函数,原来它的内部也有不为人知的内涵。
原型:
#include<pthread.h>
void pthread_exit(void *rval_ptr);
参数是个空类型的指针,其实关于空类型的意思我在前面已经介绍过了,这里不再赘述。
这个函数的功能就是使一个线程正常退出,终止线程,因为我们知道线程它是依赖进程存在的,如果在线程中使用
exit()函数退出,那么整个的进程将会退出,那么如果此时你还有一些其它需要做的事情没有完成呢,这并不是我们所希望的。
这个参数保存的是线程退出以后返回的值。当然这个参数传递的数值可以不止一个,比如 该我们想传递好几个参数怎么办呢,很快我们就能够想到我们所学过的结构体呀之类的,此时问题就来了,需要注意的问题是:这个结构所使用的内存在调用者完成调用以后必须仍然是有效的,否则就会出现非法访问内存。例如在线程栈上分配了该结构,那么其他的线程在是用这个结构时内存内容可能已经改变了。又如,线程在自己的栈上分配了一个结构然后把指向这个结构的指针传给pthread_exit(),那么当调用pthread_join的线程试图使用该结构时,这个栈有可能已经被撤销,这块内存也已令做他用。
不如我们下面看一个例子来理解:

#include<stdio.h>#include<pthread.h>#include<unistd.h>#include<stdlib.h>struct foo{    int a,b,c,d;};void printfoo(const char *s,const struct foo *fp){    printf("s is %s\n",s);    printf("structure at 0x%x\n",(unsigned)fp);    printf("foo.a=%d\n",fp->a);    printf("foo.b=%d\n",fp->b);    printf("foo.c=%d\n",fp->c);    printf("foo.d=%d\n",fp->d);}void *thr_fn1(void *arg){    struct foo foo={1,2,3,4};    printfoo("thread 1:\n",&foo);    pthread_exit((void *)&foo);}void * thr_fn2(void *arg){    struct foo foo={4,5,6,7};    printf("thread 2:ID is %d\n",pthread_self());    printfoo("thread 2\n",&foo);    pthread_exit((void *)0);}int main(void){        int err;    pthread_t tid1,tid2;    struct foo *fp;    err=pthread_create(&tid1,NULL,thr_fn1,NULL);    if(err!=0){        printf("create thread 1 is failed\n",strerror(err));        exit(1);    }    err=pthread_join(tid1,(void *)&foo);    if(err!=0)            printf("can't join thread1\n");    sleep(1);    err=pthread_create(&tid2,NULL,thr_fn2,NULL);    if(err!=0)    {        printf("create thread 2 is failed\n");         exit(1);    }    sleep(1);    printfoo("parent :\n",&fp);    exit(0);}
就比如上面这个例子:我们在线程thr_fn1里面创建了一个结构体变量foo,然后赋值并把它打印出来,最后线程调用pthread_exit退出的时候,我们想把foo这个变量返回给主线程,最后让主线程也打印出这个结构体变量中的值,但是事实上可以吗?我们看一下结果:
s is thread 1:


structure at 0xb7559380
foo.a=1
foo.b=2
foo.c=3
foo.d=4
thread 2:ID is -1219126416
s is thread 2


structure at 0xb7559380
foo.a=4
foo.b=5
foo.c=6
foo.d=7
s is parent :


structure at 0xb7559380
foo.a=-1217691028
foo.b=-1219128428
foo.c=-1217683468
foo.d=-1217691020
结果根本不是我们所想到的,其实学过C语言,这个问题也不难想,因为foo这个局部变量是在栈上面分配的,而我们知道,函数在返回或退出的时候,这些局部变量就会自动被释放,所以返回给主函数的还是一个野指针。这里我们需要知道的另一个知识点就是,每个线程之间的栈是相互独立的,每个线程都拥有自己的栈区,虽然说它们的栈都是从进程空间的栈中分配的并且共享进程的栈,但是在创建线程的时候,每个线程都从进程栈区那儿获得一个私有的栈区。
处理上面的出现的问题办法就是,使用全局结构或malloc动态分配内存;
下面是我改为全局变量的例子:
#include<stdio.h>#include<pthread.h>#include<unistd.h>#include<stdlib.h>struct foo{    int a,b,c,d;};struct foo foo={1,2,3,4};void printfoo(const char *s,const struct foo *fp){    printf("s is %s\n",s);    printf("structure at 0x%x\n",(unsigned)fp);    printf("foo.a=%d\n",fp->a);    printf("foo.b=%d\n",fp->b);    printf("foo.c=%d\n",fp->c);    printf("foo.d=%d\n",fp->d);}void *thr_fn1(void *arg){//    struct foo foo={1,2,3,4};    printfoo("thread 1:\n",&foo);    pthread_exit((void *)&foo);}void * thr_fn2(void *arg){        printf("thread 2:ID is %d\n",pthread_self());    pthread_exit((void *)0);}int main(void){        int err;    pthread_t tid1,tid2;    struct foo *fp;    err=pthread_create(&tid1,NULL,thr_fn1,NULL);    if(err!=0){            printf("create thread 1 is failed\n",strerror(err));        exit(1);    }    err=pthread_join(tid1,(void *)&fp);    if(err!=0)            printf("can't join thread1\n");    sleep(1);    err=pthread_create(&tid2,NULL,thr_fn2,NULL);    if(err!=0)    {        printf("create thread 2 is failed\n");         exit(1);    }    sleep(1);    printfoo("parent :\n",fp);    exit(0);}

运行结果如下所示:
s is thread 1:


structure at 0x804a034
foo.a=1
foo.b=2
foo.c=3
foo.d=4
thread 2:ID is -1218634896
s is parent :


structure at 0x804a034
foo.a=1
foo.b=2
foo.c=3
foo.d=4
不是就好了么,那么动态分配了,我相信学过C 的人都会,下去自己体验一下吧!其实还有一种方法就是在每个线程里定义一个结构体变量也可以,但我们的主要目的是主线程希望得到子线程返回的值。所以在使用此函数时一定要注意!这里还有一个问题是,大家看到我打印出的线程2的ID是负数,其实是我打印的时候格式的问题,只要改为%u就行了!