小白求教:线程的问题

int a = 5;
用函数 pthread_exit((void*)&a)

然后用

void *p ;
pthread_join(xx,&p);
printf("%d\n", * (int * )p);

这样打出来的数不是5呢

肯定不对的呀,pthread_join函数,第二个参数明显是二级指针。二级指针肯定要分陪内存的呀。
void *start_routine(void *args)
{
int *num = (int *)malloc(sizeof(int));
*num = 5;
cout << *num << endl;
cout << "the tid is" << pthread_self() << endl;
pthread_exit((void *)num);
}

int main ()
{

    pthread_t tid = 0;
    void *p = NULL;
    pthread_create(&tid, NULL, start_routine, NULL);
    pthread_join(tid, &p);
    cout << *((int *)p) << endl;

    return 0;

}
这样就可以了