pthread_exit

线程通过调用pthread_exit函数终止执行,就如同进程在结束时调用exit函数一样。这个函数的作用是,终止调用它的线程并返回一个指向某个对象的 指针
void pthread_exit(void* retval);
pthread_exit() 参数
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
void *print_message_function( void *ptr )
{
char *message;
message = (char *) ptr;
printf("%s \t", message);
printf("PID: %ld \n", pthread_self());
pthread_exit ("thread all done"); // 重点看 pthread_exit() 的参数,是一个字串,这个参数的 指针可以通过
// pthread_join( thread1, &pth_join_ret1);
}
main()
{
pthread_t thread1, thread2;
char *message1 = "Thread 1";
char *message2 = "Thread 2";
int iret1, iret2;
void *pth_join_ret1;//="thread1 has done!";
void *pth_join_ret2;//="thread2 has done!";
/* Create independant threads each of which will execute function */
// pthread_create return 0 if create a thread is ok!
iret1 = pthread_create( &thread1, NULL, print_message_function, (void*)"thread one_here");
iret2 = pthread_create( &thread2, NULL, print_message_function, (void*) message2);
pthread_join( thread1, &pth_join_ret1);
pthread_join( thread2, &pth_join_ret2);
//
//if(pth_join_ret1==NULL || pth_join_ret2==NULL)
//{
// printf("in %d lines \n",__LINE__);
//}
printf("Thread 1 returns: %d\n",iret1);
printf("Thread 2 returns: %d\n",iret2);
printf("pthread_join 1 returns: %s\n",(char *)pth_join_ret1);
printf("pthread_join 2 returns: %s\n",(char *)pth_join_ret2);/目的就是打印 线程退出时的返回值
exit(0);
}
//重点看上面红色注释。以前确实没注意,今天帮一个新同事调试程序才仔细看了说明
DESCRIPTION
pthread_exit terminates the execution of the calling
thread. All cleanup handlers that have been set for the
calling thread with pthread_cleanup_push(3) are executed
in reverse order (the most recently pushed handler is exe-
cuted first). Finalization functions for thread-specific
data are then called for all keys that have non- NULL val-
ues associated with them in the calling thread (see
pthread_key_create(3)). Finally, execution of the calling
thread is stopped.
The retval argument is the return value of the thread. It
can be consulted from another thread using

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值