在C语言中创建2个pthreads

问题描述 投票:0回答:1

我想做一个简单的程序,用两个独立的线程计算圆的面积和周长:第一个线程计算周长,第二个线程计算面积。

> test1.c: In function ‘main’: test1.c:32:35: warning: passing argument
> 3 of ‘pthread_create’ from incompatible pointer type
> [-Wincompatible-pointer-types]    32 |     pthread_create (&pt1 , NULL
> , area ,NULL);
>       |                                   ^~~~
>       |                                   |
>       |                                   float * (*)(void *) In file included from test1.c:3: /usr/include/pthread.h:200:15: note: expected
> ‘void * (*)(void *)’ but argument is of type ‘float * (*)(void *)’  
> 200 |       void *(*__start_routine) (void *),
>       |       ~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~ test1.c:33:35: warning: passing argument 3 of ‘pthread_create’ from incompatible
> pointer type [-Wincompatible-pointer-types]    33 |     pthread_create
> (&pt2 , NULL , circumference ,NULL);
>       |                                   ^~~~~~~~~~~~~
>       |                                   |
>       |                                   float * (*)(void *) In file included from test1.c:3: /usr/include/pthread.h:200:15: note: expected
> ‘void * (*)(void *)’ but argument is of type ‘float * (*)(void *)’  
> 200 |       void *(*__start_routine) (void *),
>       |       ~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~

这就是代码:

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

float *area (void *param)
{
int rad;
 float PI = 3.14, area;

   printf("\nEnter radius of circle: ");
   scanf("%d", &rad);

   area = PI * rad * rad;
   printf("\narea: %f ", area);
   return 0 ;
}
float *circumference (void *param)
{
   int rad;
   float PI = 3.14, cir;
   printf("\nEnter radius of circle: ");
    scanf("%d", &rad);
   cir = 2 * PI * rad;
   printf("\nCircumference : %f \n", cir);
   return 0;
}
int main ()
{
    pthread_t pt1 ;
    pthread_t pt2 ;
    pthread_create (&pt1 , NULL , area ,NULL);
    pthread_create (&pt2 , NULL , circumference ,NULL);
    pthread_join (pt1 , NULL);
    pthread_join (pt2 , NULL);
    return 0;
}
c linux pthreads
1个回答
1
投票

替换 floatvoid

void *area (void *param)

void *circumference (void *param)
© www.soinside.com 2019 - 2024. All rights reserved.