【问题标题】:array and vector multiplication using threads c language使用线程c语言进行数组和向量乘法
【发布时间】:2014-06-24 20:23:54
【问题描述】:

我正在编写一个程序,它使用线程来计算由一维数组产生的数组,所有维度都等于“n”。
每个线程都必须使用该一维数组计算产生一行数组。
我得到的输出似乎有地址值,而不是我已经作为矩阵元素输入的值。 我做错了什么?
这是我写的代码:

#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define mat_dim 5

static struct param
{
    int mat[mat_dim][mat_dim];
    int vec[mat_dim];
    int ind;
    int alter[mat_dim];
};

void *Calculate_row(struct param tid)
{
    int i;
    for (i=0; i<5; i++) { 
        tid.alter[tid.ind] = tid.alter[tid.ind]+tid.mat[tid.ind][i]*tid.vec[i];
    }
    pthread_exit((void *)&tid);
}

int main (int argc, char *argv[])
{
    pthread_t thread[mat_dim];
    pthread_attr_t attr;
    int rc;
    long t;
    void *status;
    int th_array[5][5]={{1,4,3,5,1},{4,6,2,8,5},{3,5,1,3,6},{1,5,6,2,8},{4,7,5,3,6}};
    int th_vec[5]={1,2,1,2,1};
    struct param thread_parameter;
    thread_parameter.mat[5][5]=th_array;
    thread_parameter.vec[5]=th_vec;
    int tmp[5]={0,0,0,0,0};
    thread_parameter.alter[5]=tmp;
    /* Initialize and set thread detached attribute */
    pthread_attr_init(&attr);
    pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);

    for(t=0; t<mat_dim; t++) {
        printf("Main: creating thread %ld\n", t);
        thread_parameter.ind=t;
        rc = pthread_create(&thread[t], &attr, Calculate_row,&thread_parameter); 
        if (rc) {
            printf("ERROR; return code from pthread_create() is %d\n", rc);
            exit(-1);
        }
    }

    /* Free attribute and wait for the other threads */
    pthread_attr_destroy(&attr);
    printf("the result vector is : ");
    for(t=0; t<mat_dim; t++) {
        rc = pthread_join(thread[t], NULL);
        if (rc) {
            printf("ERROR; return code from pthread_join() is %d\n", rc);
            exit(-1);
        }
        printf("%d, ",thread_parameter.alter[t]);
    }

    printf("Main: program completed. Exiting.\n");
    pthread_exit(NULL);
}

【问题讨论】:

  • "寻求调试帮助的问题(“为什么这段代码不起作用?”)必须包括所需的行为、特定问题或错误以及在问题本身中重现它所需的最短代码。没有明确的问题陈述对其他读者没有用处。”
  • 我说每个线程都必须使用该一维数组计算一行数组的生成。这是期望的行为。基本上我正在修改一维数组的元素以成为结果数组,但结果没有任何意义。毕竟,非常感谢@Antti Haapala

标签: c multithreading


【解决方案1】:

您正在调用 rc = pthread_create(&thread[t], &attr, Calculate_row,&thread_parameter); 在哪里 struct param thread_parameter;

函数为 void *Calculate_row(struct param tid) 它应该是 void *Calculate_row(struct param *tid) 作为指针传递并更改所有 .到->。

【讨论】:

  • 我试过了,完全没有帮助。结果最多仍然是负数,这根本没有意义。
【解决方案2】:

所有这些行:

thread_parameter.mat[5][5]=th_array;
thread_parameter.vec[5]=th_vec;
thread_parameter.alter[5]=tmp;

错了。数组索引从 0 到 4,因此通过寻址第 5 个元素,您超出了它们的范围。

您以错误的方式使用pthread_create。这是原型:

 int pthread_create(pthread_t *thread, const pthread_attr_t *attr,
                          void *(*start_routine) (void *), void *arg);

您的Calculate_row 接收void* 作为参数,需要将其转换为(struct param *),然后取消引用。

【讨论】:

  • 谢谢,但仍然是错误的,这些是结果数组 129100401, -1217300148, -163754450, 0, 1672224820 的值,这显然是错误的。
  • @GhaithMansour 查看我修改后的答案。寻址这些数组的第 5 个元素的行是错误的。数组索引从 0 到 4。
猜你喜欢
  • 1970-01-01
  • 2020-10-25
  • 2011-09-28
  • 1970-01-01
  • 2016-08-25
  • 2019-06-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多