pthread 基础篇 pthread_join

int pthread_join(pthread_t th, void **thread_return);

功能:挂起当前线程,等待线程th结束,并获取该线程结束时返回的数据。

th:线程ID

thread_turn:存放线程结束时返回的数据地址


使用注意事项:

1·线程结束时返回的数据地址,不能使用局部变量的地址,即栈上的地址,线程结束后,线程栈空间会被释放掉,相应的数据有可能被冲掉

2· 多个线程调用pthread_join,等待同一个线程的返回结果是未定义的


测试代码:

#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>

void *thread_proc_function(void *argements);

char *thread_return_value = "i will go, miss you baby..";

void main()
{
        pthread_t thread1;
        char    *ptr_message1   = "message 1";
        void    *ptr_return_value       = 0;
        int     iret1   = 0;

        iret1   = pthread_create(&thread1, NULL, thread_proc_function, ptr_message1);

        pthread_join(thread1, &ptr_return_value);
        printf("main get sub thread return string: %s\n", ptr_return_value);

        exit(0);
}

void *thread_proc_function(void *argements)
{
        char *message = (char *)argements;
        printf("%s\n", message);
        return thread_return_value;
}

参考资料:

1· http://zh.wikipedia.org/wiki/Native_POSIX_Thread_Library

2· http://www.yolinux.com/TUTORIALS/LinuxTutorialPosixThreads.html#BASICS


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值