2013-03-04 4 views
3

현재 pthreads를 사용하는 프로젝트에서 작업하고 있습니다. 지금까지 프로젝트는 사용자가 지정한 수의 스레드를 시작하고 각 스레드에서 작업을 수행 한 다음 닫습니다. 각 스레드는 동적으로 할당 된 메모리 배열에 저장됩니다. 나는이 사용하여 수행pthread 배열에 값을 반환

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

는 내가 다음에해야 할 것은 이러한 스레드의 반환 값을 저장할 수 있습니다 :

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

그럼 내가가 루프에서 각 스레드를 만들 수 있습니다. 내 이해는 pthread_join 반환 값을 저장할 포인터의 주소를 전달해야합니다.이 조금 혼란스러워하는 곳입니다. 나는이 시점까지 포인터로 괜찮아. 그런데 내 머리 속은 하하가 녹아있다. 이것은이 acheive하는 방법에 대한 내 생각하지만 난이 올바른지 확신 아니에요 :에

int val = *(return_val[0]); 

어떤 도움 :

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])); 
} 

가 그럼 난과 비슷한 일을 할 것입니다 반환 값을 얻을 이것은 크게 감사하겠습니다!

+0

이 게시물을 체크 아웃 하시겠습니까? http://stackoverflow.com/questions/2251452/how-to-return-a-value-from-thread-in-c – zzk

답변

5

참고이 같이 당신의 스레드에 대한 메모리를 할당되는 :

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; 
} 

그러나 후자를 선택하는 경우 가능한 메모리 누수에주의하십시오.

+0

죄송합니다. < 답변 해 주셔서 감사합니다. 정말 고맙습니다 :) –

+1

@ amura.cxg :'int' 배열을 만들고 있습니다 : sizeof (int *)가 아닌'sizeof (int) * number_of_threads'에'malloc'을 넘겨 주어야합니다. * number_of_threads' – LihO

+0

그 점을 지적 해 주셔서 감사합니다 :) –