简体   繁体   中英

Creating 2 pthreads in C

I'm trying to make a simple program that can calculate the area and circumference of a circle with two separate threads: the first pthread should calculate the circumference and the second pthread should calculate the the area. but when I try to compile it I get this message:

> 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 *),
>       |       ~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~

this is the code:

#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;
}

replace float with void

void *area (void *param)

void *circumference (void *param)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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