2013-11-25 212 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; 
}