我试图使用结构将这些矩阵传递给pthread。
矩阵分配

double **A = (double **)malloc(size_matrix * sizeof(double*));
double **B = (double **)malloc(size_matrix * sizeof(double*));
double **C = (double **)malloc(size_matrix * sizeof(double*));
for(i = 0; i < size_matrix; i++){
    A[i] = (double *)malloc(size_matrix * sizeof(double));
    B[i] = (double *)malloc(size_matrix * sizeof(double));
    C[i] = (double *)malloc(size_matrix * sizeof(double));
}

通过结构
for (t=0; t<thread_num-1; t++) {
    thread_data[t].start = thread_count;
    thread_data[t].end = thread_count + rows_per_thread-1;
    thread_data[t].A_Matrix = *A;
    thread_data[t].B_Matrix = *B;
    thread_data[t].C_Matrix = *C;
    thread_count += rows_per_thread;
}

结构
typedef struct {
int start;
int end;
double *A_Matrix;
double *B_Matrix;
double *C_Matrix;
} thread_data_t;

线程执行的例程。
void *thread_mul(void *arg)
{
thread_data_t *td = (thread_data_t *) arg;
int i,j,k;


for ( i=td->start; i<=td->end; i++) {
    for (j=0; j<size_matrix; j++){
        for(k=0;k<size_matrix;k++){

            td->*C[i][j]+=td->*A[i][k]*td->(*B[k][j]);
        }
    }

}


pthread_exit(0);
}

问题是当例程试图执行时,它在
td->*C[i][j]+=td->*A[i][k]*td->(*B[k][j]);

我得到一个错误,说它期望在->和*。
谢谢你的帮助!!

最佳答案

首先,矩阵成员的声明需要有两个星号,因为它是指向指针的指针:

typedef struct {
    int start;
    int end;
    double **A;
    double **B;
    double **C;
} thread_data_t;

语法td->*C[i][j]不正确:不需要解引用运算符(即星号*),因为方括号[]将间接寻址级别降低了一:
td->C[i][j] += (td->A[i][k]) * (td->B[k][j]);

注意:这与您的问题无关,但在需要取消对存储在struct中的指针的引用的情况下,可以对表达式的结果应用星号,例如:*(td->ptrC[i][j])这里不需要括号,因为->具有更高的优先级,但我还是把它们放进去说明发生了什么。

10-06 10:38
查看更多