1

我正在编写一个多线程程序,该程序读取矩阵文件并创建将每行 2 个矩阵相乘的线程。我只使用一个线程就设法让一切正常工作,但是当我尝试同时创建多个线程时,我遇到了问题。首先,我将结果矩阵存储在一个全局结构中,该结构包含一个结构数组,该结构数组包含一个矩阵值数组。(例如 struct matrixArray.array -> struct matrix.array)。当 pthread_create 调用的函数运行时,我的全局 matrixArray 丢失了所有数据。因此,我尝试将 matrixArray 传递给 pthread_create 调用的函数。但是,我不断收到错误消息:表达式必须具有结构或联合类型。这是当我尝试访问被调用函数内部的结构时,

这是我的结构:

typedef struct matrix{

    int rows;
    int cols;
    int multRow; // The "MULTIPLIED ROW" This is for determing which row the current thread needs to use for multiplication. This only applies for Matrix A in each set.
    int size;
    int set; // This is for which set the matrix belongs to.
    char letter; // This is for labeling the matrices A B and C
    int * array;
    unsigned int * threadID; // Array containing the thread ids that are used to create the result

} matrix;


typedef struct matrixArray{

    int size;
    matrix * array;

} matrixArray;

这是主要功能:

int main(int argc, char *argv[])
{

    pthread_t * tid; /* the thread identifier */
    pthread_attr_t attr; /* set of attributes for the thread */
    int i; // Counter
    int aIndex; // Index of the current 'A' matrix being multiplied.
    int rows,cols;

    // Checke to make sure we have the correct number of arguments supplied
    // when running the program.
    if(argc < 1){
            printf("Error: You did not provide the correct number of arguments.\n\n");
            return 0;
    }

    // Read the file and create the matrices
    readFile();


    // Initialize the result matrix before we start creating threads that use it.
    mtxResults = newMatrixArray();

    // Get the default attributes
    pthread_attr_init(&attr);

    // Set the current set to be mutliplied to 1
    currentSet = 1;

    // Create a new matrixArray to pass to the threads
    //struct matrixArray *mtxPassed = malloc(sizeof(struct matrixArray));
    struct matrixArray *mtxPassed = newMatrixArray();
    memcpy(mtxPassed, &mtxResults, sizeof(struct matrixArray));

    // Allocate size of tid array based on number of threads
    tid = malloc(threads * sizeof(pthread_t));

    // Create the threads.
    for(i = 0; i < threads; i++){
            //pthread_create(&tid[i], &attr, runner, argv[1]);
            pthread_create(&tid[i], &attr, runner, mtxPassed);

            // Increment currentSet when the current row evalutated
            // in the current set is equal to the total number of rows available.
            aIndex = ((currentSet * 2) - 2);

            if(mtx.array[aIndex].multRow == mtx.array[aIndex].rows){
                    currentSet++;
            }
    }

    // Wait for threads to finish
    for(i = 0; i < threads; i++){
            pthread_join(tid[i], NULL);
    }

    // Print the matrices
    //printMatrices();

} // End of main()

这是函数运行器,被 pthread_create 调用:

// The thread will begin control in this function
void *runner(void *param)
{

    struct matrixArray *mtxA = newMatrixArray();
    mtxA = (struct matrixArray *)param;

    printf("mtxPassed.size = %i\n",mtxA.size);

    // Do the matrix multiplication for a single row
    matrixMultiply(currentSet, (unsigned int)pthread_self(), mtxA);

    pthread_exit(0);

}

最初,当我将 mtxA 结构传递给 matrixMultiply 函数时发生了错误,但后来我尝试直接在 runner 内部访问它以查看它是否在那里工作。它仍然不是。我在 "printf("mtxPassed.size = %I\n",mtxA.size);" 处收到错误 除了前两行跑步者,我还尝试了“struct matrixArray *mtxA = (struct matrixArray *)param;” 那也没有用。

这是错误:

[nsltg2@lewis assign2]$ cc -pthread -lpthread assign2.c
assign2.c(602): error: expression must have struct or union type
  printf("mtxPassed.size = %i\n",mtxPassed.size);
                             ^

我非常感谢您能提供的任何帮助。太感谢了!

4

0 回答 0