0

我对 C 语言相当陌生。我写了一段代码,它创建了两个线程来计算两个不同的结果。代码实际上可以工作,但编译显示错误,我想知道实际上是什么错误。谁能帮我?我是C新手,所以我想这可能是一个非常愚蠢的错误......

错误:

q3.c: In function ‘main’:
q3.c:63:2: warning: passing argument 3 of ‘pthread_create’ from incompatible pointer type [enabled by default]
In file included from q3.c:5:0:
/usr/include/pthread.h:225:12: note: expected ‘void * (*)(void *)’ but argument is of type ‘double * (*)(void *)’
q3.c:64:9: warning: passing argument 3 of ‘pthread_create’ from incompatible pointer type [enabled by default]
In file included from q3.c:5:0:
/usr/include/pthread.h:225:12: note: expected ‘void * (*)(void *)’ but argument is of type ‘double * (*)(void *)’

编码:

#include <stdlib.h>
#include <pthread.h>
#include <string.h>
#include<stdio.h>
#include<math.h>
#define PI 3.1415

int i, x, n;

double *sin_x(void* dimension);
double *cos_x(void* dimension);
int fact(int);
struct data
{
  int ang_deg;
  int no_of_terms;
};

int fact(int num)
{
  int f = 0;

  if (num == 1)
    return 1;
  else
    f = num * fact(num - 1);

  return f;
}

int main(int argc, char argv[])
{
  printf("\nEnter x and n:\t");
  scanf("%d %d", &x, &n);

  struct data
  {
    int ang_rad;
    int no_of_terms;
  };

  struct data dimension;  // automatic allocation, all fields placed on stack

  dimension.ang_rad = x;
  dimension.no_of_terms = n;

  pthread_t thrd1, thrd2;
  int thret1, thret2;

  thret1 = pthread_create(&thrd1, NULL, sin_x, (void *) &dimension);
  thret2 = pthread_create(&thrd2, NULL, cos_x, (void *) &dimension);

  pthread_join(thrd1, NULL );
  pthread_join(thrd2, NULL );

  //printf("\nthret1 = %d\n", thret1);
  //printf("thret2 = %d\n", thret2);
  sleep(5);
  printf("Parent Thread exiting...\n");

  exit(1);

  return 0;
}

double *sin_x(void* dimension)
{
  struct data* dim = (struct data*) dimension;
  int ang_deg = dim->ang_deg;
  int no_of_terms = dim->no_of_terms;

  //int ang_deg, int no_of_terms
  int term, j;
  double value = 0.0, ang_rad = 0.0;
  ang_rad = (double) ang_deg * PI / 180;

  for (term = 1, j = 2; term < no_of_terms * 2; term += 2, j++)
  {
    value += (double) pow(-1.0, j) * pow(ang_rad, term) / fact(term);
  }

  printf("\nSin(%d) = %f", ang_deg, value);
  double *a = &value;

  return a;
}

double *cos_x(void* dimension)
{
  struct data* dim = (struct data*) dimension;
  int ang_deg = dim->ang_deg;
  int no_of_terms = dim->no_of_terms;

  int term, j;
  double value = 1.0, ang_rad = 0.0;
  ang_rad = (double) ang_deg * PI / 180;

  for (term = 2, j = 1; term <= no_of_terms; term += 2, j++)
  {
    value += (double) pow(-1.0, j) * pow(ang_rad, term) / fact(term);
  }

  printf("\nCos(%d) = %f", ang_deg, value);
  double *a = &value;

  return a;
}
4

2 回答 2

3

这不是错误,而是警告。

你的功能是:

双 *sin_x (void *dimension);
双 *cos_x (void *dimension);

由于函数的名称是指向函数的指针,因此类型sin_x为 :(double * (*) (void *)字面意思是:指向以 avoid*作为参数并返回 a的函数的指针double *),对于cos_x.

但是pthread_create正在等待类型void * (*) (void *)(指向void*作为参数并返回的函数的指针void*)。

由于double*void*都是指针,它们具有相同的大小(例如 64b 上的 8 个字节),因此编译器只会警告您。

于 2013-10-07T11:36:38.910 回答
2

变化:

  • 替换sleep()pthread_exit()。睡眠对于同步来说不是一个好主意。
  • 更正了main()的参数。它应该是int main(void)int main(int argc, char**argv)或等价的。
  • 删除了里面不必要的结构main()
  • 将返回类型更改fact()size_t,这将允许您计算比int类型更多的阶乘。
  • 重写阶乘函数以使用记忆。这是计算阶乘的更有效方法,因为您不需要重新计算之前已经计算过的阶乘。数组大小为 64K,您可以更改。就目前而言,它可以计算前 64K 阶乘。
  • pthread_您应该为功能 添加错误检查。
  • 自线程退出以来删除了pthread_join()调用。main()一旦最后一个线程退出,该进程将自然退出。
  • 删除了线程值,因为您没有使用它们。如果您确实想知道计算值,可以将另一个成员添加到 struct dimension 并将结果存储在其中。此方法将允许从线程函数返回复杂值。

--

这是完成上述所有更改的功能齐全的代码。有关更多信息,请阅读pthread_create()其他功能的手册。

#include <stdlib.h>
#include <pthread.h>
#include <string.h>
#include<stdio.h>
#include<math.h>
#define PI 3.1415

int i, x, n;
void *sin_x( void* dimension );
void *cos_x( void* dimension );
size_t fact( int );

size_t tab[64*1024] = {1};
struct data
            {
                int ang_deg;
                int no_of_terms;
            };  

size_t fact( int num )
{
      size_t f = 0;

      if ( tab[num]) return tab[num];

      f = num * fact( num - 1 );
      tab[num] = f;

      return f;
}

int main (int argc, char **argv)
{ 
        printf( "\nEnter x and n:\t" );
        scanf( "%d %d", &x, &n );

        struct data dimension;  // automatic allocation, all fields placed on stack

        dimension.ang_deg = x;
        dimension.no_of_terms= n;

        pthread_t thrd1, thrd2;
        int thret1, thret2; 

        thret1 = pthread_create(&thrd1, NULL, sin_x, &dimension);
        thret2 = pthread_create(&thrd2, NULL, cos_x, &dimension);   

        printf("Parent Thread exiting...\n");
        pthread_exit(0);
}

void *sin_x( void* dimension )
{
    struct data* dim = dimension;
    int ang_deg = dim->ang_deg;
    int no_of_terms = dim->no_of_terms;
    int term, j;
    double value = 0.0, ang_rad = 0.0;
    ang_rad = ( double ) ang_deg * PI / 180;

    for ( term = 1, j = 2;term < no_of_terms*2;term += 2, j++ )
        {
            value += ( double ) pow( -1.0, j ) * pow( ang_rad, term ) / fact( term );
        }
    printf("\nSin(%d) = %f", ang_deg, value);
    return 0;
}

void *cos_x( void* dimension )
{
    struct data* dim = dimension;
    int ang_deg = dim->ang_deg;
    int no_of_terms = dim->no_of_terms;
    int term, j;
    double value = 1.0, ang_rad = 0.0;
    ang_rad = ( double ) ang_deg * PI / 180;

    for ( term = 2, j = 1;term <= no_of_terms;term += 2, j++ )
        {
            value += ( double ) pow( -1.0, j ) * pow( ang_rad, term ) / fact( term );
        }

    printf("\nCos(%d) = %f", ang_deg, value);
    double *a = &value;
    return 0;
}
于 2013-10-07T11:31:57.263 回答