You are not logged in.
Pages: 1
Hi,
I am trying to pass a routine to "pthread_create()" function, which returns an integer as its return value. I do not know how to access this returned value. Can you please help me?
here is the way I do the creation:
///////////////////////////////
int status;
pthread_t tid;
status=pthread_create(&tid,NULL,(void*)start,NULL);
and the start is as this way:
int start()
{
int count=10;
return count;
}
/////////////////////////////////////
Offline
Would you please write me the exact syntax of how this can be done? now I assign the pthread_join return value to an integer but it is zero.
and further more changing the (void*)start to (int)(void*)start results in warnings.
Offline
According to your guidance and the man page of pthread_join now I do the following:
void* start()
{
int counter;
counter=10;
pthread_exit(&counter);
}
main()
{
void* r;
....
pthread_join(tid,&r);
....
}
but now that I print r there is not the desired result. it is a big numbet such as -2375165318376
Offline
wow!it is working now.
in the start I simply write it this way:
void* start()
{
int counter=10;
return (void*) counter;
}
and in main:
...
int counter;
pthread_join(tid, (void*) & counter);
and it is perfect now.
..
Offline
Your last problem, before you fixed it, was that 'counter' is allocated on the
stack and disappears when the function exits.
Well, that and the fact he was printing the pointer value instead of dereferencing it,
I'd assume... The way he's doing it now, it doesn't need dereferencing, of course,
because it's just using the pointer to hold an int value...
However, this will be a problem if you ever compile on a 64-bit platform:
int counter;
pthread_join(tid, (void*) & counter);
You'll trash 4 bytes of your stack with that on a 64-bit system, because void* will be
a 64-bit value and int will only be 32-bit... You really should use a void* variable,
and then cast that back to int after filling it in...
Offline
Pages: 1