c - 如何使用 pthread、pthread_exit 将 * 转换为 double/float

标签 c unix pthreads pthread-join pthread-exit

我需要创建一个计算递归的程序(对于某些序列)。当我使用 int 并 decleare 递归时,它可以计算没有 float 的值(例如斐波那契序列,它仅返回中性数字)。但是,当尝试使用基于除法(带有 float )的序列时,它会显示如下错误:

错误:无法转换为浮点类型 pthread_exit((void*)(float)wynik;

我应该如何更改代码(或者实际上是一个函数 *ciag,因为问题出在该函数上),以使其接受 float ?

工作正常的函数(使用 int)

int* fibo(int n){

   int wynik;
   int* n1;
   if (n==0) wynik=0;
   else if (n==1) wynik=1;
   else wynik =(int)fibo((int)(n-1))+(int)fibo((int)(n-2));
   return (int*)wynik;
   pthread_exit((void*)wynik);
}

还有我遇到的问题(使用 float,但当我尝试使用 double 时也会发生同样的情况)

    #include <unistd.h>

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

#define COUNT 2

float *ciag(int n) {
    float wynik;

    if(n == 0)
        wynik = -1;
    else
        wynik = ((float)ciag(n - 1)*(n + 1))/(float)ciag(n - 1)*(float)ciag(n - 1)*(float)ciag(n - 1);

    return(float *)wynik;
    pthread_exit((void *)wynik);
}

void *drugi_watek(void* wynik) {
    int i = 1;  

        while(i == 0) {
        printf("#");
        fflush(stdout);
        usleep(300000);
        pthread_exit((void*)wynik);
    }
}

int main() {
    pthread_t watek_1, watek_2;
    int n;
    float wynik;
    printf("Podaj numer ciagu: ");
    scanf("%d", &n); 

    pthread_create(&watek_1, NULL,(void*)&ciag, n);
    pthread_create(&watek_2, NULL, &drugi_watek, NULL);

    if(!pthread_join(watek_1,(void**)&wynik))
    {
    pthread_cancel(watek_2);
    }

    printf("Element numer %f ciagu: %f\n", &n, &wynik);


    return 0;
}

最佳答案

您不能直接将 float 转换为 void *,反之亦然。

最干净的方法是在某处为float分配空间——无论是从堆还是调用者的堆栈上——并让线程函数存储float code> 值转换为指向变量(float * 可以轻松地与 void * 相互转换)。如果您采用此路线并在堆栈上分配值,则需要确保调用者的堆栈帧保持存在,直到线程完成。

由于要调用的函数是递归的,所以将其作为线程函数太麻烦了。最好将其设为一个单独的(普通)函数,该函数采用 int 参数并返回 float 。然后创建一个包装函数,该函数将成为 pthread_create 的目标。

由于您还需要将参数 int 传递给函数,因此最简单的方法是分配一个 struct 来包含参数和返回值(一个 union 也可以工作,因为您实际上并不同时需要参数和返回值)。下面是演示该模式的示例程序:

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

static float ciag(int n)
{
    float wynik;

    if(n == 0)
        wynik = -1;
    else
        wynik = (ciag(n - 1)*(n + 1))/ciag(n - 1)*ciag(n - 1)*ciag(n - 1);

    return wynik;
}

typedef struct {
    int i;
    float f;
} if_t;

static void *ciag_thread(void *vp)
{
    if_t *ifp = vp;
    // Obtain argument from the structure and put the result back into the structure
    ifp->f = ciag(ifp->i);
    return vp;
}

int main()
{
    pthread_t watek_1;

    int n = 4;

    // Obtain n however you like. Then place argument into structure allocated 
    // on the stack
    if_t arg;
    arg.i = n;

    // Pointer to structure is implicitly convertible to (void *)
    pthread_create(&watek_1, NULL, ciag_thread, &arg);
    pthread_join(watek_1, NULL);
    printf("Thread returned %f\n", arg.f);
    return 0;
}

还有一个注释。您的代码似乎表明第一个线程上的 pthread_join 有时可能会失败。这里不会发生这种情况。尽管对于较大的 n 值,由于函数的四次性质,可能需要很长时间才能完成。

关于c - 如何使用 pthread、pthread_exit 将 * 转换为 double/float,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53395262/

相关文章:

linux - 删除文件中的文件结尾 (EOF)

c - 如何通过在 C 中使用 pthread_join 来控制线程数?

c - 如何更改线程函数内的 void* 参数

c - 初始化包含指向其自身类型的指针的常量结构

c - 为什么if语句返回true?

android - 如何从终端打开 URL

c - 父局部变量充当三个 child 之间的共享变量

c - VSCode 不构建和调试 pthreads 代码

c++ - 在 Linux 中更改线程优先级和调度程序

c - 在程序中使用全新的 C 变量