c - pthread Return Values to an Array -


i working on project uses pthreads. project far starts user specified number of threads , work on each thread closes. each thread stored in dynamically allocated array of memory. using:

threads = malloc(number_of_threads * sizeof(pthread_t));

then create each thread in for-loop:

pthread_create(&(threads[i]), null, client_pipe_run, (void *) &param[i]); 

what need next store return values of these threads. understanding need pass pthread_join address of pointer want have return value stored in. little confused. i'm fine pointers point brain kind of has melt down haha. idea on how acheive i'm not confident correct:

int *return_vals = malloc(sizeof(int) * number_of_threads); for(i = 0; i< number_of_threads; i++) { pthread_join(&(threads[i]),(void *) &(return_vals[i])); } 

then return value similar to:

int val = *(return_val[0]); 

any on appreciated!

note allocating memory threads this:

threads = malloc(number_of_thread * sizeof(pthread_t)); 

but return values do:

int *return_vals = malloc(sizeof(int *)); 

i.e. number of threads should taken in count here too:

int *return_vals = malloc(number_of_thread * sizeof(int)); 

then can either cast return value void*:

void *foo(void *arg) {     int = 7;     return (void*)i; }  int main(void) {     int = 0;     int thread_count = 3;     pthread_t* threads = malloc(thread_count * sizeof(pthread_t));     int *return_vals = malloc(thread_count * sizeof(int));      // create threads:     for(i = 0; < thread_count; ++i)         pthread_create(&threads[i], null, &foo, null);      // wait untill finish work:     for(i = 0; < thread_count; ++i)         pthread_join(threads[i], (void**) &return_vals[i]);      // print results:     for(i = 0; < thread_count; ++i)         printf("thread %d returned: %d\n", i, return_vals[i]);      // clean up:     free(return_vals);     free(threads);      return 0; } 

or can make sure code doesn't make presumptions size of type you're returning being less or equal sizeof(void*) , allocate memory return value dynamically within thread:

void *foo(void *arg) {     int* ret = malloc(sizeof(int));     *ret = 7;     return ret; }  int main(void) {     int = 0;     int thread_count = 3;     pthread_t* threads = malloc(thread_count * sizeof(pthread_t));      // array of pointers return values of type int:     int **return_vals = calloc(thread_count, sizeof(int*));      // create threads:     for(i = 0; < thread_count; ++i)         pthread_create(&threads[i], null, &foo, null);      // wait untill finish work:     for(i = 0; < thread_count; ++i)         pthread_join(threads[i], (void**) &return_vals[i]);      // print results:     for(i = 0; < thread_count; ++i)         printf("thread %d returned: %d\n", i, *return_vals[i]);      // clean up:     for(i = 0; < thread_count; ++i)         free(return_vals[i]);     free(return_vals);     free(threads);      return 0; } 

but in case chose latter one, careful possible memory leaks might end with.


Comments

Popular posts from this blog

ruby - Trying to change last to "x"s to 23 -

jquery - Clone last and append item to closest class -

c - Unrecognised emulation mode: elf_i386 on MinGW32 -