将结构传递给c中的pthread无效。尝试访问传递的结构结果时出错:表达式必须具有结构或联合类型
我正在编写一个多线程程序,它读入一个矩阵文件,并创建将每行2个矩阵相乘的线程。我只使用了一个线程就成功地让所有的东西正常工作,但是当我试图同时创建多个线程时,我遇到了问题。首先,我将结果矩阵存储在一个全局结构中,该结构包含一个包含矩阵值数组的结构数组。例如,struct matrixArray.array->struct matrixArray.array。当pthread_create调用的函数运行时,我的全局矩阵阵列丢失了所有数据。因此,我尝试将矩阵数组传递到pthread_create调用的函数中。然而,我不断得到错误:表达式必须具有struct或union类型。这是当我试图访问被调用函数中的结构时,尽管我将其声明为struct matrixArray 以下是我的结构:将结构传递给c中的pthread无效。尝试访问传递的结构结果时出错:表达式必须具有结构或联合类型,c,multithreading,struct,pthreads,C,Multithreading,Struct,Pthreads,我正在编写一个多线程程序,它读入一个矩阵文件,并创建将每行2个矩阵相乘的线程。我只使用了一个线程就成功地让所有的东西正常工作,但是当我试图同时创建多个线程时,我遇到了问题。首先,我将结果矩阵存储在一个全局结构中,该结构包含一个包含矩阵值数组的结构数组。例如,struct matrixArray.array->struct matrixArray.array。当pthread_create调用的函数运行时,我的全局矩阵阵列丢失了所有数据。因此,我尝试将矩阵数组传递到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()
最初,错误是在我将mtxA结构传递给matrixMultiply函数时发生的,但后来我尝试在runner中直接访问它,以查看它是否在那里工作。现在还不是。
我在printfmtxPassed.size=%I\n,mtxA.size处获得错误;我还尝试了struct matrixArray*mtxA=struct matrixArray*param,而不是runner的前两行;这也不管用
以下是错误:
[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);
^
我非常感谢你能提供的任何帮助。非常感谢你 也许你应该使用->insead of。要通过pointerHmm访问结构成员,这似乎确实修复了类型的错误,但是,它仍然没有传递全局结构中的数据。你知道如何从我的全局结构中获取数据以在我的runner函数中工作吗?你的线程进程在前两行中泄漏内存。这不是Java。在主进程中:memcpymtxPassed,&mtxResults,sizeofstruct matrixArray;你是从你不拥有的记忆中复制的。&mtxResults参数错误。它应该是mtxResults,事实上,一开始就不应该存在。你应该是复制构造mtxPassed。考虑到所有这些以及matrixArray类的定义,您也完全违反了。简言之,这大部分都是错误的。还有,什么是新矩阵阵列;?您可能打算在那里执行malloc,除非有一个名为newMatrixArray的函数返回malloced值。谢谢!是的,我在其他网站上发现了这些东西,但我不确定它应该做什么。我刚弄明白为什么我的全局结构丢失了数据。在我已经填满它之后,我正在重新初始化它。在main函数中删除行mtxResults=newMatrixArray修复了我丢失数据的问题。谢谢你的帮助!
[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);
^