b2科目四模拟试题多少题驾考考爆了怎么补救
b2科目四模拟试题多少题 驾考考爆了怎么补救

pthread_join/pthread_exit的用法解析

电脑杂谈  发布时间:2019-07-31 07:16:57  来源:网络整理

pthread exit_pthread_pthread self

官方说法:

函数pthread_join用来等待一个线程的结束。函数原型为:

extern int pthread_join __p ((pthread_t __th , void **__thread_return))。extern int pthread_join __p (pthread_t __th, void **__thread_return)。extern int pthread_join __p ((pthread_t __th, void **__thread_return))。

第一个参数为被等待的线程标识符,第二个参数为一个用户定义的指针,它可以用来存储被等待线程的返回值。这个函数是一个线程阻塞的函数,调用它的线程将一直等待到被等待的线程结束为止,当函数返回时,被等待线程的资源被收回。一个线程的结束有两种途径,一种是象我们上面的例子一样,函数结束了,调用它的线程也就结束了;

另一种方式是通过函数pthread_exit来实现。它的函数原型为:

extern void pthread_exit __P ((void *__retval)) __attribute__ ((__noreturn__));

第22行调用pthread_join()函数等待线程的结束,通过函数的第一个参数指定要等待的线程,第二个参数用来接收线程函数返回值。当调用pthread_create()创建新线程后当前线程从pthread_create()返回继续往下执行,而新线程执行的代码由函数指针start_routine决定start_routine函数接受一个参数是由pthread_create()通过参数arg传递的,这个参数的类型是void *按什么类型解释由用户自己决定。当一个线程调用dll函数时,该dll函数要查看线程的堆栈,以便检索它传递的参数,并将线程的堆栈用于它需要的任何局部变量。

解析:

pthread_join用于等待一个线程的结束,也就是主线程中要是加了这段代码,就会在加代码的位置卡主,直到这个线程执行完毕才往下走。

pthread_exit用于强制退出一个线程(非执行完毕退出)pthread exit,一般用于线程内部。

结合用法:

一般都是pthread_exit程内退出,然后返回一个值。这个时候就跳到主线程的pthread_join了(因为一直在等你结束)pthread exit,这个返回值会直接送到pthread_join,实现了主与分线程的通信。

注意事项:

这个线程退出的返回值的格式是void*,无论是什么格式都要强转成void*才能返回出来主线程(pthread_exit((void*)tmp);),而这个时候pthread_join就去接这个值,我们传进去一个void*的地址也就是&(void*),传地址进去接值是接口类函数常用的做法,有同样效果的做法是引用&,但是这个做法一来值容易被误改,二来不规范,所以定义一个类型然后把地址传进去修改value。回到题目,这里返回的void*是一个指针类型,必须强转成对应的指针才能用。

pthread_pthread exit_pthread self

举个例子,如果是char* = “mimida”;传出来的tmp,必须(char*)tmp一下。

是错误的,因为这里new返回的是指向一个有3个元素的一维int型数组的指针,而p是一个指向int型数据的指针。上面的内联函数同它的非内联函数相比,仅仅是多了一个关键字inline,它们在功能上并没有区别:前者也是有两个形参,一个double型,一个int型,返回值是double型,且两个形参相除后所得的商作为结果返回。编译器会说不能把int*型转化为int[]型,因为用new开辟了一段内存空间后会返回这段内存的首地址,所以要把这个地址赋给一个指针,所以要用int *p=new int[len]。

最重要的一点,你定义的类型和最后出来的类型一定要一致,不然很容易出现问题。也就是你定义了int*,最后强转出来的一定是*(int*)。

别void* a = (void*)10;这种诡异的格式(我就中过招),一开始是什么就转成什么!(这个规则同时也适用于线程数据里的set和get)

实例如下:

[cpp]

<prename="code"class="cpp">/*example.c*/

#include<stdio.h>

#include<pthread.h>

voidthread1(chars[])

{

printf("Thisisapthread1.\n");

printf("%s\n",s);

pthread_exit((void*)"thefirstreturn!");//结束线程,返回一个值。

pthread exit_pthread self_pthread

}

voidthread2(chars[])

{

int*a=new(46666);

<spanstyle="white-space:pre"></span>printf("Thisisapthread2.\n");

printf("%s\n",s);

pthread_exit((void*)a);

}

/**************mainfunction****************/

intmain(void)

{

pthread_tid1,id2;

void*a1,*a2;

inti,ret1,ret2;

pthread exit_pthread_pthread self

chars1[]="Thisisfirstthread!";

chars2[]="Thisissecondthread!";

pthread_create(&mythread[n],null,(void *)&sendout,(void *)&n)。 ret=pthread_create(&id1,null,(void *)pthread1, null)。ret=pthread_create(&id1,null,(void *)pthread1, null)。

handle[1] = ( handle )_beginthreadex(null , 0, threadfun, ( void *)&thread2, 0, null )。 handle[1] = ( handle )_beginthreadex(null , 0, threadfun, ( void*)&thread2, 0, null )。 ret=pthread_create(&id2,null,(void *)pthread2, null)。

if(ret1!=0){

printf("Createpthread1error!\n");

exit(1);

}

pthread_join(id1,&a1);

printf("%s\n",(char*)a1);

if(ret2!=0){

printf("Createpthread2error!\n");

exit(1);

}

pthread exit_pthread self_pthread

printf("Thisisthemainprocess.\n");

pthread_join(id2,&a2);

printf("%s\n",*(int*)a2);

return(0);

}

运行结果:

[****@XD**** c]$ ./example

This is a pthread1.

This is first thread!

the first return!

This is the main process.

This is a pthread2.

This is second thread!

46666


本文来自电脑杂谈,转载请注明本文网址:
http://www.pc-fly.com/a/jisuanjixue/article-117140-1.html

    相关阅读
      发表评论  请自觉遵守互联网相关的政策法规,严禁发布、暴力、反动的言论

      热点图片
      拼命载入中...