這是因爲每一次
void pthread_exit(void *ret);
將從所以它永遠要簡單地返回其指針傳遞用了pthread_exit()線程函數被調用。
現在在
int pthread_join(pthread_t tid, void **ret);
將從創建線程地方所以在這裏接受返回的指針總是叫你需要雙指針 ..
我認爲這個代碼將幫助你瞭解這個
#include<stdio.h>
#include<pthread.h>
#include<stdlib.h>
void* thread_function(void)
{
char *a = malloc(10);
strcpy(a,"hello world");
pthread_exit((void*)a);
}
int main()
{
pthread_t thread_id;
char *b;
pthread_create (&thread_id, NULL,&thread_function, NULL);
pthread_join(thread_id,(void**)&b); //here we are reciving one pointer
value so to use that we need double pointer
printf("b is %s",b);
free(b); // lets free the memory
}
但是爲什麼在`pthread_exit`中定義一個`void *`類型,它總是`NULL`或者其他一些常量值 – stonestrong 2014-02-13 08:22:37