2013-11-25 256 views
3

好吧我試圖通過structpthread_create函數在pthread中傳遞一對數字。但我通過我得到當函數被調用的數量和數量是不同的,隨機將struct傳遞給pthread作爲參數

這裏是struct

struct Pairs { 
    long i,j; 
}; 

而且裏面主要

void main() 
{ 
    long thread_cmp_count = (long)n*(n-1)/2; 
    long t,index = 0; 
    struct Pairs *pair; 
    pair = malloc(sizeof(struct Pairs)); 

    cmp_thread = malloc(thread_cmp_count*sizeof(pthread_t)); 
    for(thread = 0;(thread < thread_cmp_count); thread++){ 
     for(t = thread+1; t < n; t++){ 
      (*pair).i = thread; 
      (*pair).j = t; 
      pthread_create(&cmp_thread[index++], NULL, Compare, (void*) pair); 

     } 
    } 

    for(thread= 0;(thread<thread_cmp_count); thread++){ 
     pthread_join(cmp_thread[thread], NULL); 
    } 

    free(cmp_thread); 
} 

和功能比較

void* Compare(void* pair){ 
    struct Pairs *my_pair = (struct Pairs*)pair; 
    printf("\nThread %ld, %ld", (*my_pair).i, (*my_pair).j); 
    return NULL; 
} 

我得到的數字,它也是rando米

Thread 0,2 
Thread 1,2 
Thread 2,3 
Thread 2,3 
Thread 2,3 
Thread 2,3 

我是否通過了struct錯誤?

+0

請注意,在C,結構訪問通過指針被永遠不會寫成'(* foo).bar':而是使用'foo-> bar'。 – unwind

回答

12

這是因爲您正在向所有pthreads傳遞相同的指針。

當您調用pthread_create(..., (void*) pair)時,您正在將指針傳遞給新線程,但在下一次迭代中,您將覆蓋該內存(可能在新線程提取這些值之前)。

long thread_cmp_count = (long)n*(n-1)/2; 
    long t,index = 0; 
    struct Pairs *pair; 

    cmp_thread = malloc(thread_cmp_count*sizeof(pthread_t)); 
    for(thread = 0;(thread < thread_cmp_count); thread++){ 
     for(t = thread+1; t < n; t++){ 
      // allocate a separate pair for each thread 
      pair = malloc(sizeof(struct Pairs)); 
      (*pair).i = thread; 
      (*pair).j = t; 
      pthread_create(&cmp_thread[index++], NULL, Compare, (void*) pair); 

     } 
    } 

    for(thread= 0;(thread<thread_cmp_count); thread++){ 
     pthread_join(cmp_thread[thread], NULL); 
    } 

    free(cmp_thread); 

void* Compare(void* pair){ 
    struct Pairs *my_pair = (struct Pairs*)pair; 
    printf("\nThread %ld, %ld", (*my_pair).i, (*my_pair).j); 

    // free that memory after it has been used 
    free (pair); 
    return NULL; 
} 
+1

你有問題,但沒有解決。我用一個指針作爲一個結構數組來避免重疊。不管怎麼說,還是要謝謝你。 – Jos

2

問題解決了。問題是重疊。使用指針類型的arraystruct偶也就迎刃而解了

下面是正確的代碼

long thread_cmp_count = (long)n*(n-1)/2; 
long t,index = 0; 
Pair * pair; 
pair = malloc(thread_cmp_count*sizeof(Pair)); 

free(thread_handles); 

thread_handles = malloc(thread_cmp_count*sizeof(pthread_t)); 
for(thread = 0;(thread < n-1); thread++){ 
    for(t = thread+1; t < n; t++){ 
     (pair+index)->i = thread; 
     (pair+index)->j = t; 
     pthread_create(&thread_handles[index], NULL, Compare, (void*) (pair+index)); 
     index++; 
    } 
} 
for(thread= 0;(thread<thread_cmp_count); thread++){ 
    pthread_join(thread_handles[thread], NULL); 
} 

free(thread_handles); 

而且功能比較

void* Compare(void* pair){ 
    long t,i,j; 
    Pair *my_pair = (Pair*)pair; 
    i = my_pair->i; 
    j = my_pair->j; 
    printf("\n.................................................................."); 
     if((x_array[i] < x_array[j])&&(x_array[i] != x_array[j])){ 
      w_array[i] = 0; 
      printf(
       "\nThread T(%ld,%ld)" 
       " compares x[%ld] = %ld and x[%ld] = %ld," 
       " and writes 0 to w[%ld]", i, j, 
       i,x_array[i], 
       j,x_array[j], 
       i); 
     } 
     else if((x_array[i] > x_array[j])&&(x_array[i] != x_array[j])){ 
      w_array[j] = 0; 
      printf(
       "\nThread T(%ld,%ld)" 
       " compares x[%ld] = %ld and x[%ld] = %ld," 
       " and writes 0 to w[%ld]", i, j, 
       i,x_array[i], 
       j,x_array[j], 
       j); 
     } 
     else 
      return NULL; 
    return NULL; 
}