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*)’第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
{
// Do something.
std :: cout << "\nTime to enjoy!";
}
pthread_mutex_unlock (&mutexA);
}
}发布于 2012-08-01 11:25:07
如果您查看手册页,您将看到函数参数是
void *(*start_routine) (void *)也就是说,指向接受一个void *参数并返回void *的函数的指针。
若要消除错误,请将函数更改为返回void *,并在不进行类型转换的情况下传递它。如果您不关心这个值,那么线程函数的返回可以是一个简单的return NULL。
发布于 2012-08-01 11:23:20
(void *) &functionA会将函数指针functionA (类型为void (*)(void*) )转换为简单的void*。后者不能再次转换为第一个,所以编译器会报告一个错误。这是为什么不应该使用C风格的转换的原因之一。
使用pthread_create (&A, NULL, functionA, NULL);代替。
另外,线程函数的返回类型应该是void*,而不是void。所以将void functionA(void*)改为void* functionA(void*)。
发布于 2012-08-01 11:27:39
使用
pthread_create (&A, NULL, functionA, NULL); 而不是演员。
另外,您用来传递给pthread_create的函数应该返回一个void*,因此为了避免以后出现任何问题,请考虑更改函数签名以适应这种情况。
https://stackoverflow.com/questions/11758121
复制相似问题