2013-12-07 5 views
2

C 스레드와 그 반환 값에 대한 질문이 있습니다. 내 목표는 시작 번호와 끝 번호 사이의 모든 소수를 찾는 것입니다. 나는 4 개의 스레드를 가지며, 각 스레드는 범위의 1/4을 수행한다.C 스레드 및 결합

그래서 예를 들어, 소수는 쓰레드 1이 소수 발견

  • 1 ~ 100 (1) 사이 - 스레드
  • 75-51 3 스레드
  • 50-26 2 스레드
  • 25 4 76 - 100

모든 소수는 배열에 저장되며 소수를 계산하는 함수가 있습니다. 100 - 나는

pthread_join(tids[i], ptr); 

는 모든 소수, 하나의 결합 된 배열에 대한 포인터 ptr에 할 스레드에 가입 할 때

내 질문은,? 하나 개의 큰 배열로 100 - 나는 그것이 1의 모든 소수를 인쇄 할 수있는 값

printf("%d", ptr[i]); 

을 인쇄하는 루프를 사용하는 경우

의미?

4 개의 개별 어레이를 결합합니까?

당신에게

답변

2

phtread_join()가 pthread_exit()에 전달 상응하는 어떤 스레드 PTR을 반환합니다 감사합니다. 각 쓰레드는 독립적으로 작동하고 자신의 소수 집합을 계산하므로 각 쓰레드는 자체 배열을 생성해야하고 모든 쓰레드가 합쳐지면 각 배열의 결과를 출력합니다.

struct comp_result { 
    unsigned *p_arr; 
    unsigned count; 
}; 

내가 잠금없이 방법을 설명합니다 :

compute_thread (...) { 
    ... 
    struct comp_result *p_res = malloc(sizeof(struct comp_result));   
    p_res->p_arr = NULL; 
    p_res->count = 0; 
    for (num = start; num < end; num++) { 
     if (is_prime(num)) { 
       p_res->count++; /// increment number of primes and expand our storage 
       p_res->p_arr = realloc(p_res->p_arr, p_res->count*sizeof(int)); 
       p_res->p_arr[p_res->count-1] = num; // save prime in result array 
     } 
    } 

    // pass pointer to per-thread result data 
    pthread_exit(p_res); 
} 


main() { 
     .... // create threads 
     .... 
     for (i = 0; i < num_of_threads; i++) { 
      struct comp_result *p_res; 
      // retrieve and print array from i-thread 
      pthread_join(tids[i], &p_res); 
      for (num = 0; num < p_res->count; num++) { 
       printf(" %d ", p_res->p_arr[num]); 
      } 
      free(p_res->p_arr); 
      free(p_res); 
     } 
} 

잠금과 그림이 하나 더 필요 소수와 계수의 결과 집합 반환 할 수 있으려면, 우리는 자신의 구조체 형식을 사용해야합니다 struct 유형이므로 각 스레드에 결과 공유 데이터에 대한 포인터를 전달합니다.

struct control { 
    unsigned start; 
    unsigned end; 
    struct comp_result *p_res; 
} 

compute_thread (ptr) { 
    struct control *p_cont = (struct control*)ptr; 
    // retrieve the pointer to shared, but be accurate with it! 
    struct comp_result *p_res = p_cont->p_res; 

    // initialize lock 
    pthread_mutex_init(&p_res->lock, NULL); 

    ... 
    for (num = p_cont->start; num < p_cont->end; num++) { 
     if (is_prime(num)) { 
       pthread_mutex_lock(&p_control->lock); 
       // modify shared data with locking 
       p_res->count++; /// increment number of primes and expand our storage 
       p_res->p_arr = realloc(p_res->p_arr, p_res->count*sizeof(int)); 
       p_res->p_arr[p_res->count-1] = num; // save prime in result array 
       pthread_mutex_unlock(&p_control->lock); 
     } 
    } 

    pthread_exit(NULL); 
} 


main() { 
     //create one shared data and initialize it: 
     struct comp_result *p_res = malloc(sizeof(struct comp_result));   

     p_res->p_arr = NULL; 
     p_res->count = 0; 

     for (i = 0; i < num_of_threads; i++) { 
      // create per-thread control data: 
      struct control *p_control = malloc(sizeof(struct control));   
      p_control->start = 
      p_control->end = 
      p_control->p_res = p_res; 
      pthread_crate(&tids[i], NULL, compute_thread, p_control); 
     } 
     .... 
     for (i = 0; i < num_of_threads; i+++) { 
      pthread_join(tids[i], NULL); 
     } 
     // now all threads are completed and we are ready to read result: 
     for (num = 0; num < p_res->count; num++) { 
      printf(" %d ", p_res->p_arr[num]); 
     } 
} 
+0

감사합니다. 또한 void * ptr = NULL을 선언 할 수도 있습니다. 그런 다음 내 join 함수를 수행하십시오 pthread_join (tids [i], &ptr); 그럴까요? – user2817240

+0

이 경우 ptr은 배열을 가리키는 변수이므로 for 루프를 사용하여 포인터를 내 구조로 캐스트 할 수 있습니다 배열에 소수를 출력 할 수 있습니까? – user2817240

+0

그래서 4 개의 배열을 가지므로 ptr 변수를 사용하여 하나의 배열을 만들고 그 값을 반환 할 수 있습니까? – user2817240