我正在设计一个俄罗斯方块的翻拍,需要一个计时器函数,同时运行作为输入函数。我使用pthread来实现这一点,但是当我调用
pthread_create(&timer, NULL, Timer(), NULL);
我收到一个错误,声称没有匹配的函数来调用pthread_create(),尽管我的标头中包含了<pthread.h>。
我注意到另一个人问了几乎相同的问题,here。然而,我成功地在另一台计算机上创建了pthread,而没有做任何建议给那个人的事情。
下面是我遇到问题的源代码。我不是要求你重写它,而是告诉我哪里出了问题。我会做一些研究来修复我的代码。
#include <pthread.h>
#include <iostream>
#include <time.h>
void *Timer(void) { //I have tried moving the asterisk to pretty much every
//possible position and with multiple asterisks. Nothing works
time_t time1, time2;
time1 = time(NULL);
while (time2 - time1 <= 1) {
time2 = time(NULL);
}
pthread_exit(NULL);
}
int main() {
pthread_t inputTimer;
pthread_create(&inputTimer, NULL, Timer(), NULL); //Error here
return 0;
}谢谢
发布于 2012-08-15 08:05:36
您需要传递Timer函数的地址,而不是它的返回值。因此
pthread_create(&inputTimer, NULL, &Timer, NULL); // These two are equivalent
pthread_create(&inputTimer, NULL, Timer, NULL);pthread_create期望它的第三个参数具有以下类型:void *(*)(void*);即接受void*的单个参数并返回void*的函数。
发布于 2012-08-15 08:05:44
您需要向pthread_create传递您希望它调用的函数的地址,而不是您希望它调用的函数的返回值:
pthread_create(&inputTimer, NULL, Timer, NULL);此外,您的函数必须具有以下签名void* (void*),因此必须将其更改为:
void *Timer(void*) {
time_t time1, time2;
time1 = time(NULL);
while (time2 - time1 <= 1) {
time2 = time(NULL);
}
pthread_exit(NULL);
}https://stackoverflow.com/questions/11962550
复制相似问题