简体   繁体   中英

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. I tried to remove (void*) and make the conversion implicit but I still got the same error

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*) . You need to define probSAT as:

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).

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);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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