gpt4 book ai didi

c - C中的简单多线程错误?

转载 作者:太空宇宙 更新时间:2023-11-04 05:41:03 25 4
gpt4 key购买 nike

我是 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;
}

最佳答案

这不是错误,而是警告。

你的功能是:

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

因为函数名是指向函数的指针,sin_x 的类型是:double * (*) (void *)void* 作为参数并返回 double * 的函数),与 cos_x 相同。

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

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

关于c - C中的简单多线程错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19223609/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com