简体   繁体   English

错误:从'int(*)(void *)'到'void *(*)(void *)'的转换无效

[英]error: invalid conversion from ‘int (*)(void*)’ to ‘void* (*)(void*)’

I amtrying to spawn pthreads and send an integer as the argument but I am getting the following error when casting the argument to void. 我正在尝试生成pthreads并发送一个整数作为参数但是在将参数转换为void时出现以下错误。 I tried to remove (void*) and make the conversion implicit but I still got the same error 我试图删除(void *)并隐式转换,但我仍然遇到同样的错误

error: invalid conversion from ‘int (*)(void*)’ to ‘void* (*)(void*)’ [-fpermissive]
   rc=pthread_create(&threads[i],NULL,probSAT, (void *)&threads_args[i]);

void Solver::p(char** argc)
{
    argvp=argc;
    pthread_t threads[NTHREADS];
    int threads_args[NTHREADS];
    int i=0;
    int rc;

    for(i=0;i<5;i++)
    if(order_heap.empty())
        v[i]=i+1;
    else
        v[i]=(order_heap.removeMin())+1;
    for (i=0;i<32;i++)
    {
        threads_args[i]=i;
        rc=pthread_create(&threads[i],NULL,probSAT, (void *)&threads_args[i]);
    }
    pthread_exit(NULL);
    return;


}

A function defined as int (*)(void*) is not compatible with one defined as void* (*)(void*) . 定义为int (*)(void*)函数与定义为void* (*)(void*)的函数不兼容。 You need to define probSAT as: 您需要将probSAT定义为:

void *probSAT(void *);

If you want to effectively return an int from this function, you can either return the address of a global variable or (the better option) allocate space for an int and return a pointer to that (and ensure you deallocate it when you join the thread). 如果你想从这个函数有效地返回一个int ,你可以返回一个全局变量的地址,或者(更好的选项)为int分配空间并返回一个指向它的指针(确保你在加入线程时解除分配它) )。

void *probSAT(void *param) {
    int *rval = malloc(sizeof(int));
    if (rval == NULL) {
        perror("malloc failed");
        exit(1);
    }
    ....
    *rval = {some value};
    return rval;
}


void get_thread_rval(pthread_t thread_id) 
{
    void *rval;
    int *rval_int;
    if (pthread_join(thread_id, &rval) != 0) {
        perror("pthread_join failed");
    } else {
        rval_int = rval;
        printf("thread returned %d\n", *rval_int);
        free(rval_int);
    }
}

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

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