错误:无效转换从'void *'到'void *(*)(void *)' - pthreads [英] error: invalid conversion from ‘void*’ to ‘void* (*)(void*)’ - pthreads

查看:643
本文介绍了错误:无效转换从'void *'到'void *(*)(void *)' - pthreads的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  anisha @ Linux的y3pi:〜> g ++ conditionVarTEST.cpp -Wall 

conditionVarTEST.cpp:在函数'int main()'中:
conditionVarTEST.cpp:33:53:错误:从'void *'无效转换为' void *(*)(void *)'
conditionVarTEST.cpp:33:53:error:初始化参数3'int pthread_create(pthread_t *,const pthread_attr_t *,void *(*)(void *),void *)'
conditionVarTEST.cpp:34:53:error:从'void *'无效转换为'void *(*)(void *)'
conditionVarTEST.cpp:34:53:error:初始化'int pthread_create(pthread_t *,const pthread_attr_t *,void *(*)(void *),void *)''

$ b $的参数3 b




第33行是这样的:

  pthread_create (& A,NULL,(void *)& functionA,NULL); 






functionA 是这样的:

void functionA(void *);






其定义如下:

  void functionA (void * argA)
{
while(1)
{
pthread_mutex_lock(& mutexA);

if(count <0)
{
pthread_cond_wait(& conditionVariableA,& mutexA);
}
else
{
//做点什么。
std :: cout<< \\ \\时间享受!;
}
pthread_mutex_unlock(& mutexA);
}
}


解决方案

你看看手册页面,你会看到函数参数是

  void *(* start_routine)(void *)

即指向一个函数的指针,它需要一个 void * 参数并返回 void * void * ,并且不要传递它类型铸造它。如果你不关心这个值,那么线程函数的返回值可以是一个简单的 return NULL


anisha@linux-y3pi:~> g++ conditionVarTEST.cpp -Wall

conditionVarTEST.cpp: In function ‘int main()’:
conditionVarTEST.cpp:33:53: error: invalid conversion from ‘void*’ to ‘void* (*)(void*)’
conditionVarTEST.cpp:33:53: error:   initializing argument 3 of ‘int pthread_create(pthread_t*, const pthread_attr_t*, void* (*)(void*), void*)’
conditionVarTEST.cpp:34:53: error: invalid conversion from ‘void*’ to ‘void* (*)(void*)’
conditionVarTEST.cpp:34:53: error:   initializing argument 3 of ‘int pthread_create(pthread_t*, const pthread_attr_t*, void* (*)(void*), void*)’


Line number 33 is this:

pthread_create (&A, NULL, (void *) &functionA, NULL);


Declaration of functionA is this:

void functionA (void*);


Its definition is:

void functionA (void* argA)
{
    while (1)
    {
        pthread_mutex_lock (&mutexA);

        if (count < 0)
        {
            pthread_cond_wait (&conditionVariableA, &mutexA);
        }
        else
        {
            // Do something.
            std :: cout << "\nTime to enjoy!";
        }
        pthread_mutex_unlock (&mutexA);
    }
}

解决方案

If you look at the manual page you will see that the function argument is

void *(*start_routine) (void *)

That is, a pointer to a function which takes one void * argument and returns void *.

To get rid of your errors, change your function to return void *, and pass it without type-casting it. The return from the thread function can be a simple return NULL if you don't care about the value.

这篇关于错误:无效转换从'void *'到'void *(*)(void *)' - pthreads的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆