UNIX Socket FAQ

A forum for questions and answers about network programming on Linux and all other Unix-like systems

You are not logged in.

#1 2009-04-15 09:01 AM

parisa1984
Member
Registered: 2009-04-12
Posts: 29

Re: start routine in thread creation

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

#2 2009-04-15 10:25 AM

i3839
Oddministrator
From: Amsterdam
Registered: 2003-06-07
Posts: 2,239

Re: start routine in thread creation

You can get to it via pthread_join().

The function is supposed to return a void* though, you probably should
cast around that to treat it as an int.

Offline

#3 2009-04-15 11:09 AM

parisa1984
Member
Registered: 2009-04-12
Posts: 29

Re: start routine in thread creation

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

#4 2009-04-15 11:31 AM

i3839
Oddministrator
From: Amsterdam
Registered: 2003-06-07
Posts: 2,239

Re: start routine in thread creation

Read the pthread_join() manpage for the exact syntax.

Casting start to void* works as well, I missed you did that.

Offline

#5 2009-04-15 02:14 PM

parisa1984
Member
Registered: 2009-04-12
Posts: 29

Re: start routine in thread creation

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

#6 2009-04-15 05:58 PM

parisa1984
Member
Registered: 2009-04-12
Posts: 29

Re: start routine in thread creation

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

#7 2009-04-15 06:51 PM

i3839
Oddministrator
From: Amsterdam
Registered: 2003-06-07
Posts: 2,239

Re: start routine in thread creation

Your last problem, before you fixed it, was that 'counter' is allocated on the
stack and disappears when the function exits.

Offline

#8 2009-04-15 08:01 PM

RobSeace
Administrator
From: Boston, MA
Registered: 2002-06-12
Posts: 3,839
Website

Re: start routine in thread creation

int counter;
pthread_join(tid, (void*) & counter);

Offline

Board footer

Powered by FluxBB