您的位置:首页 > 其它

pthread 基础篇 pthread_join

2013-05-21 20:43 232 查看
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;

}

参考资料:

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

http://www.yolinux.com/TUTORIALS/LinuxTutorialPosixThreads.html#BASICS
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: