小编Jos*_*Jos的帖子

将struct作为参数传递给pthread

好的我正试图传递一对数字structpthread_create运行pthread.但是我传递的数字和调用函数时得到的数字是不同且随机的

这里是 struct

struct Pairs {
    long i,j;
};
Run Code Online (Sandbox Code Playgroud)

内部主要

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);
}
Run Code Online (Sandbox Code Playgroud)

和功能比较

void* Compare(void* pair){
    struct Pairs *my_pair = (struct …
Run Code Online (Sandbox Code Playgroud)

c multithreading struct pthreads

6
推荐指数
1
解决办法
4万
查看次数

标签 统计

c ×1

multithreading ×1

pthreads ×1

struct ×1