简体   繁体   English

将int函数转换为void *

[英]Casting int function to void*

I have a problem with casting returned integer value to void pointer. 我有一个问题,将返回的整数值转换为void指针。 Have tried some options from this site but my problem seems to still haven't been resolved. 尝试过这个网站的一些选项,但我的问题似乎仍未解决。 Although the program compiles with no code errors I'm getting a segmentation fault. 虽然程序编译没有代码错误,但我遇到了分段错误。 Am I blind and are there some mistakes in my code? 我是盲人,我的代码中有些错误吗?

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

int ackermann(int a, int b)
{
    if(a==0)
        return a+1;
    else if(a>0 && b==0)
    {
        return ackermann(a-1, 1);
    }
    else if(a>0 && b>0)
    {
        return ackermann(a-1,ackermann(a,(b-1)));
    }
}

int main(int argc, char* argv[])
{
    int a = atoi(argv[1]);
    int b = atoi(argv[2]);
    int c = ackermann(a,b);
    void *ptr = &c;
    pthread_t mythread;
    if(pthread_create(&mythread, NULL, ptr, NULL))
    {
        printf("Could not create a thread\n");
    }

    pthread_exit(NULL);
    return 0;
}

As was mentioned in the comments, you're not actually calling the function ackermann in a separate thread. 正如评论中提到的那样,你实际上并没有在一个单独的线程中调用函数ackermann What you are doing is calling the function directly from main , storing the result in an int , and passing a pointer to that int as the third parameter to pthread_create , which is supposed to be a pointer to the function to run. 你正在做的是直接从main调用函数,将结果存储在int ,并将指向该int的指针作为第三个参数传递给pthread_create ,它应该是指向要运行的函数的指针。

Right now, ackermann does not have the appropriate signature to be passed to pthread_create . 现在, ackermann没有相应的签名传递给pthread_create A function that starts a new thread should be declared like this: 启动新线程的函数应该声明如下:

void *my_thread_function(void *parameter);

Given that ackermann is called recursively, it would be cleaner to pass aa wrapper function to pthread_create and have that wrapper call ackermann rather than modifying ackermann to match the above signature. 鉴于ackermann是递归调用的,将包装器函数传递给pthread_create并使该包装器调用ackermann而不是修改ackermann以匹配上述签名会更清晰。

Because you need to pass multiple parameters to your thread function, you'll need to create a struct which contains all the parameters and pass a pointer to that struct to thread function. 因为您需要将多个参数传递给线程函数,所以您需要创建一个包含所有参数的struct ,并将指向该struct的指针传递给线程函数。

You can also store the return value in this struct so that the function which started the thread has access to it. 您还可以将返回值存储在此结构中,以便启动该线程的函数可以访问它。

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

int ackermann(int a, int b)
{
    if(a==0) {
        return a+1;
    } else if(a>0 && b==0) {
        return ackermann(a-1, 1);
    } else if(a>0 && b>0) {
        return ackermann(a-1,ackermann(a,(b-1)));
    }
    // if none of the above conditions are true, no value is returned
    // better check for this
}

struct ackermann_params {
    int a;
    int b;
    int result;
};

void *ackermann_thr(void *arg)
{
    struct ackermann_params *params = arg;
    params->result = ackermann(params->a, params->b);
    return NULL;
}

int main(int argc, char* argv[])
{
    struct ackermann_params params;
    if (argc < 3) {
        printf("invalid number of arguments\n");
        exit(1);
    }
    params.a = atoi(argv[1]);
    params.b = atoi(argv[2]);
    pthread_t mythread;
    if(pthread_create(&mythread, NULL, ackermann_thr, &params))
    {
        perror("Could not create a thread\n");
        exit(1);
    }
    if (pthread_join(mythread, NULL)) {
        perror("join failed\n");
        exit(1);
    }
    printf("result=%d\n", params.result);

    return 0;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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