我正在编写一个多线程程序,它读入矩阵文件并创建将每行2个矩阵相乘的线程。我设法只使用一个线程让一切正常工作,但当我尝试同时创建多个线程时,我遇到了问题。首先,我将结果矩阵存储在一个全局结构中,该结构包含一个包含矩阵值数组的struct数组。 (例如struct matrixArray.array - > struct matrix.array)。当我的pthread_create调用的函数运行时,我的全局matrixArray丢失了所有数据。所以,我试图将matrixArray传递给pthread_create调用的函数。但是,我不断收到错误:表达式必须具有struct或union类型。这是当我尝试访问被调用函数内的结构时,尽管我将其声明为struct matrixArray。
以下是我的结构:
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);
^
我非常感谢您提供的任何帮助。非常感谢你!