I’m designing a remake of tetris and need a timer function that runs at the same time as an input function. I’m using pthreads to achieve this, but when I make the call to
pthread_create(&timer, NULL, Timer(), NULL);
I receive an error claiming there is no matching function for call to pthread_create() despite including <pthread.h> in my header.
I noticed that another person asked pretty much the same question here. However, I managed to successfully create pthreads on another computer without doing any of what was suggested to that person.
Below is the source code that I am having problems with. I am not asking for you to rewrite it, but rather inform me what’s wrong. I’ll do the research to fix my code.
#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;
}
Thank you
You need to pass an address of your
Timerfunction, not it’s return value. Thuspthread_createexpects it’s third argument to have the following type:void *(*)(void*); i.e. a function taking a single argument ofvoid*and returningvoid*.