次のようにスレッドにメモリを割り当てていることに注意してください。
threads = malloc(number_of_thread * sizeof(pthread_t));
ただし、戻り値については次のようにします。
int *return_vals = malloc(sizeof(int *));
つまり、ここでもスレッド数をカウントする必要があります。
int *return_vals = malloc(number_of_thread * sizeof(int));
void*
次に、戻り値を次のようにキャストできます。
void *foo(void *arg) {
int i = 7;
return (void*)i;
}
int main(void) {
int i = 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; i < thread_count; ++i)
pthread_create(&threads[i], NULL, &foo, NULL);
// wait untill they finish their work:
for(i = 0; i < thread_count; ++i)
pthread_join(threads[i], (void**) &return_vals[i]);
// print results:
for(i = 0; i < thread_count; ++i)
printf("Thread %d returned: %d\n", i, return_vals[i]);
// clean up:
free(return_vals);
free(threads);
return 0;
}
または、コードが返す型のサイズがそれ以下であると推定しないことを確認しsizeof(void*)
、戻り値のメモリをスレッド内で動的に割り当てることができます。
void *foo(void *arg) {
int* ret = malloc(sizeof(int));
*ret = 7;
return ret;
}
int main(void) {
int i = 0;
int thread_count = 3;
pthread_t* threads = malloc(thread_count * sizeof(pthread_t));
// array of pointers to return values of type int:
int **return_vals = calloc(thread_count, sizeof(int*));
// create threads:
for(i = 0; i < thread_count; ++i)
pthread_create(&threads[i], NULL, &foo, NULL);
// wait untill they finish their work:
for(i = 0; i < thread_count; ++i)
pthread_join(threads[i], (void**) &return_vals[i]);
// print results:
for(i = 0; i < thread_count; ++i)
printf("Thread %d returned: %d\n", i, *return_vals[i]);
// clean up:
for(i = 0; i < thread_count; ++i)
free(return_vals[i]);
free(return_vals);
free(threads);
return 0;
}
ただし、後者を選択した場合は、最終的に発生する可能性のあるメモリ リークに注意してください。