tags:

views:

53

answers:

2

I am attempting to make the following call,

PID = pthread_create(&t, NULL, schedule_sync(sch,t1), NULL);

schedule_sync returns a value, I would like to be able to grab that value, but from what Ive read about pthread_create, you should pass a "void" function. Is it possible to get the return value of schedule_sync, or am I going to have to modify some kind of parameter passed in?

Thanks for the help!

+2  A: 

pthread_create returns an <errno.h> code. It doesn't create a new process so there's no new PID.

To pass a pointer to your function, take its address with &.

pthread_create takes a function of form void *func( void * ).

So assuming schedule_sync is the thread function,

struct schedule_sync_params {
    foo sch;
    bar t1;
    int result;
    pthread_t thread;
} args = { sch, t1 };

int err = pthread_create( &args.thread, NULL, &schedule_sync, &args );
 .....

schedule_sync_params *params_ptr; // not necessary if you still have the struct
err = pthread_join( args.thread, &params_ptr ); // just pass NULL instead
 .....

void *schedule_sync( void *args_v ) {
   shedule_sync_params *args = args_v;
   ....
   args->result = return_value;
   return args;
}
Potatoswatter
onaclov2000
So this might be due to my limited knowledge, but if a function is void, can it "return" args? also I think I kind of see where you're going with this, and it looks like it would work if I can massage the code into a form similar, just to be sure I understand the way I read it pthread_join means essentially give priority to the function passed in over the calling thread, (I.E. make sure that one does some work right away instead of the processor taking up all it's time on the "main")
onaclov2000
@onaclov: It's not `void` at all, it returns a `void*` which is convertible to and from any pointer type. `pthead_join` doesn't operate on priorities, it simply waits until the other thread has finished and gives you its return value.
Potatoswatter
+1  A: 

schedule_sync should return void* which can be anything. You can grab the value with pthread_join.

//Function shoulde be look like this
void* schedule_sync(void* someStructureWithArguments);

//Call pthread_create like so
pthread_create(&t, NULL, schedule_sync, locationOfSomeStructureWithArguments);

When thread terminates

pthread_joint(&t, locationToPutReturnValue);

Don't have a development enviroment, so I can't get you the exact sequence just yet, but this will hopefully get you started.

Dennis Miller
Looks like Potatoswatter beat me to it.
Dennis Miller