我不知道为什么它不返回我输入的值。我知道不是void* arg
因为它打印了正确的数字,但我不知道。
代码:
#include <unistd.h>
#include <stdio.h>
#include <pthread.h>
#include <stdlib.h>
#include <sys/types.h>
void* stampa(void* arg) {
float dato = *(float*)arg;
printf("Read: %f\n", dato);
pthread_exit((void*)&dato);
}
// Main
int main() {
system("clear");
pthread_t miot;
int creathread;
float x;
float *status;
printf("Number: ");
scanf("%f", &x);
creathread = pthread_create(&miot, NULL, stampa, (void*)&x);
if (creathread != 0) {
printf("Error\n");
}
pthread_join(miot, (void*)&status);
printf("Returned: %f\n", *status);
return 0;
}
结果:
Number: 10
Read: 10.000000
Returned: 0.000000