在 C 中创建 2 个 pthread
Creating 2 pthreads in C
我正在尝试编写一个简单的程序,它可以使用两个单独的线程计算圆的面积和周长:第一个 pthread 应该计算周长,第二个 pthread 应该计算面积。
但是当我尝试编译它时,我收到了这条消息:
> 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;
}
将float
替换为void
void *area (void *param)
void *circumference (void *param)
我正在尝试编写一个简单的程序,它可以使用两个单独的线程计算圆的面积和周长:第一个 pthread 应该计算周长,第二个 pthread 应该计算面积。 但是当我尝试编译它时,我收到了这条消息:
> 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;
}
将float
替换为void
void *area (void *param)
void *circumference (void *param)