pthread_exit的返回值没有被pthread_join拿到,为什么?
pthread_join的man说,它会拿到pthread_exit的返回值:
--------------------
if retval is not NULL, then pthread_join() copies the exit status of the target thread (i.e., the value that the target thread supplied to
pthread_exit(3)) into the location pointed to by *retval. If the target thread was canceled, then PTHREAD_CANCELED is placed in *retval.
--------------------
于是我写了个小程序来验证:
#include <assert.h>
#include <pthread.h>
#include <unistd.h>
#include <iostream>
using namespace std;
void* fun(void*)
{
cout << "fun" << endl;
int i=20;
pthread_exit(&i);
}
int main()
{
pthread_t pt;
cout << "main" << endl;
int ret=pthread_create(&pt,NULL,fun,NULL);
assert(ret==0);
int *pi;//pthread return?
sleep(1);
cout << "join" << endl;
pthread_join(pt,(void**)&pi); // get from pthread_exit
cout << "i=" << *pi << endl;
return 0;
}