当前位置: 技术问答>linux和unix
怎么得到线程调用的返回值
来源: 互联网 发布时间:2016-06-20
本文导语: 我把我的分全部送上了。 | 给你一个例子: #include #include #include #include void * func(void *argc) { void *s = malloc(100); sprintf(s, "I am a thread, my id is %u.", pthread_self()); pt...
我把我的分全部送上了。
|
给你一个例子:
$ gcc pthread.c -lpthread -g
$ ./a.out
I am a thread, my id is 3086760816.
注意: 使用pthread_exit(void *retval);时:
The value pointed to by retval should not be located on the calling thread’s stack, since the contents of that stack are undefined after the thread terminates.
#include
#include
#include
#include
void * func(void *argc) {
void *s = malloc(100);
sprintf(s, "I am a thread, my id is %u.", pthread_self());
pthread_exit(s);
}
int main(int argc, char *argv[]) {
pthread_t pid;
void *retval = NULL;
pthread_create(&pid, NULL, func, NULL);
pthread_join(pid, &retval);
printf("%sn", retval);
free(retval);
return 0;
}
$ gcc pthread.c -lpthread -g
$ ./a.out
I am a thread, my id is 3086760816.
注意: 使用pthread_exit(void *retval);时:
The value pointed to by retval should not be located on the calling thread’s stack, since the contents of that stack are undefined after the thread terminates.