将结构体作为参数传递给pthread

9

我正在尝试通过struct将一对数字传递给pthread_create函数在pthread中。但是,我传递的数字与函数调用时获得的数字不同且随机。

这是struct

struct Pairs {
    long i,j;
};

在 main 函数内部
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);
}

And函数比较

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

我得到的是一个数字,而且它也是随机的。

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

我是否错误地传递了struct


请注意,在C语言中,通过指针访问结构体的方式不应该写成(*foo).bar,而是应该使用foo->bar - unwind
2个回答

29
这是因为你将相同的指针传递给了所有pthread。
当你调用 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

4
问题已解决。问题出在重叠上。使用指针作为struct Pairs类型的array解决了它。
以下是正确的代码。
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);

还有 Compare 函数

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

我尝试了相同的代码,但是出现了错误dereferencing pointer to incomplete type ‘struct Pairs’ - Naila Akbar

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接