简体   繁体   English

调用pthread_create错误-'void'之前的预期主表达式

[英]Calling pthread_create error - expected primary expression before 'void'

This is a Matrix multiplication code. 这是一个矩阵乘法代码。 It creates a thread to multiply each row of the first matrix to the second matrix and saves the result in matrix C . 它创建一个线程以将第一矩阵的每一行与第二矩阵相乘,并将结果保存在矩阵C中 It gives an error in the pthread_create line expected primary-expression before 'void' . expected primary-expression before 'void'的pthread_create行expected primary-expression before 'void'给出错误。
I run this code on ubunto 13.10 virtual machine. 我在ubunto 13.10虚拟机上运行此代码。
Thanks in advance. 提前致谢。

#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>

struct matrices
{
    int matrixA[10][10];
    int matrixB[10][10];
    int matricC[10][10];
    int r1,r2,c1,c2;
}*ptr;
int p;
void *matrixMul(void *);

int main()
{
    int i=0,j=0;
    pthread_t threads[10];
    ptr=(struct matrices*)malloc(sizeof(struct matrices));
    printf("Enter size of first matrix(Rows then Columns)");
    scanf("%d",&(ptr->r1));
    scanf("%d",&(ptr->c1));
    printf("Enter elements of first array : ");
    for(i=0; i<ptr->r1; i++)
    {
        for(j=0; j<ptr->c1; j++)
        {
            scanf("%d",&ptr->matrixA[i][j]);

        }
    }
    printf("Enter size of second matrix(Rows then Columns)");
    scanf("%d",&(ptr->r2));
    scanf("%d",&(ptr->c2));
    if(ptr->c1!=ptr->r2)
    {
        printf("Dimensions ERRORR! ");
    }
    else
    {
        printf("Enter elements of second array : ");
        for(i=0; i<ptr->r2; i++)
        {
            for(j=0; j<ptr->c2; j++)
            {
                scanf("%d",&ptr->matrixB[i][j]);

            }
        }
        for(i=0;i<ptr->r1;i++)
        {
            for(j=0;j<ptr->c2;j++)
            {
                ptr->matricC[i][j]=0;
            }
        }
        for (p=0;p<ptr->r1;p++)
        {
            **********pthread_create(&threads[p],NULL, *matrixMul,void &p);**********
        }
        for(i=0;i<ptr->r1;i++)
        {
            pthread_join(threads[i],NULL);
        }
        for(i=0;i<ptr->r1;i++)
        {
            for(j=0;j<ptr->c2;j++)
            {
                printf("%d",ptr->matricC[i][j]);
            }
        }
    }
    return 0;
}
void *matrixMul(void *rownum)
{
    int *i;
    int n=0,m=0;
    i=(int*)rownum;
    for(n=0;n<ptr->c2;n++)
    {
        for(m=0;m<ptr->c1;m++)
        {
            ptr->matricC[*i][n]+=(ptr->matrixA[*i][m])*(ptr->matrixB[m][n]);
        }
    }
    return NULL;
}

Your code contains minor error, but the logic is correct, don't worry. 您的代码包含一些小错误,但是逻辑是正确的,请放心。
I downloaded the code and tested it on my machine, so please note the following: 我下载了代码并在计算机上对其进行了测试,因此请注意以下几点:

This line should be written this way... 这行应该这样写...

pthread_create(&threads[i],NULL, matrixMul, &i);

Because according to the specs of pthread library that pthread_create should take void pointer to the runner function and void pointer to the parameter. 因为根据pthread库的规范, pthread_create应该使用指向运行函数的void指针和指向参数的void指针。 You don't need to add (void *) because you already declared your runner function matrixMul as void * . 您不需要添加(void *)因为您已经声明了运行器函数matrixMulvoid *
Your primary error here was (void) &i and it should be &i only as you already delcaired this parameter as void * in the runner function's prototype. 在这里的主要错误(void) &i ,并且应该是&i因为您已经在runner函数的原型中将此参数视为void * You shall too pass the runner function like this &matrixMul . 您也应该通过&matrixMul这样的运行器函数。

Some other notes: "Code Review" 其他注意事项:“代码审查”

  1. You shouldn't put your logic in the else statement, you can simply after printf("Dimensions ERRORR! "); 您不应该将逻辑放在else语句中,而只需在printf("Dimensions ERRORR! "); write exit(-1); exit(-1); because this is basically what you do if the dimensions error. 因为基本上是尺寸错误时您要执行的操作。
  2. Check for the return value (status) of pthread_create and pthread_join 检查pthread_createpthread_join的返回值(状态)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM