Linux 错误:从“void*”到“void*(*)(void*)”的无效转换 - pthreads
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11758121/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
error: invalid conversion from ‘void*’ to ‘void* (*)(void*)’ - pthreads
提问by Aquarius_Girl
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:
第 33 行是这样的:
pthread_create (&A, NULL, (void *) &functionA, NULL);
Declaration of functionA
is this:
声明functionA
是这样的:
void functionA (void*);
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);
}
}
采纳答案by Some programmer dude
If you look at the manual pageyou 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 *
.
也就是说,指向一个函数的指针,该函数接受一个void *
参数并返回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.
要消除您的错误,请将您的函数更改为 return void *
,并在不进行类型转换的情况下传递它。return NULL
如果您不关心值,则线程函数的返回可能很简单。
回答by Zeta
(void *) &functionA
will cast your function pointer functionA
which is of type void (*)(void*)
to a simple void*
. The later can't be converted to the first again, so the compiler reports an error. This is one of the reasons why you shouldn't use C-style casts.
(void *) &functionA
将您的函数指针functionA
类型 void (*)(void*)
转换为简单的void*
. 后者不能再转换为第一个,所以编译器报错。这是您不应该使用 C 样式强制转换的原因之一。
Use pthread_create (&A, NULL, functionA, NULL);
instead.
使用pthread_create (&A, NULL, functionA, NULL);
来代替。
Also, the return type of a thread function should be void*
, not void
. So change void functionA(void*)
to void* functionA(void*)
.
此外,线程函数的返回类型应该是void*
,而不是void
。所以void functionA(void*)
改为void* functionA(void*)
.
回答by mathematician1975
Use
用
pthread_create (&A, NULL, functionA, NULL);
instead of casting.
而不是铸造。
Also the function you use to pass to pthread_create should return a void*
so to avoid any problems later, consider changing the function signature to accomodate this.
此外,您用来传递给 pthread_create 的函数应该返回一个void*
so 以避免以后出现任何问题,请考虑更改函数签名以适应这一点。
回答by Torsten Robitzki
And as you are using a C++ compiler, you should use a function with C binding, as pthread_create expects a C function:
当您使用 C++ 编译器时,您应该使用带有 C 绑定的函数,因为 pthread_create 需要一个 C 函数:
extern "C" void* functionA (void*);
C++ and C may have the same calling conventions on your current platform, but there is no guaranty, that this will be the case on other platforms or will be so in the future.
C++ 和 C 在您当前的平台上可能具有相同的调用约定,但不能保证在其他平台上或将来会如此。