0

I have a problem with timer in C. I have to call a function every 3 sec (for e.g. To put down Hello world! every 3 sec). It must not wait on calling function. I'm doing a game, so function will be used to make random coordinates, but I must be able to move my player around.

Any hints?

1 Answers1

1

If you are in a Linux enviornment, you could consider something like this:

void hello_word_func(int signum)
{
    printf ("Hello World!\n");
    signal (SIGALRM, hello_word_func);
    alarm (3);
}

signal (SIGALRM, hello_word_func);
alarm (3);

Another option, which will eat more of your CPU's resources (busy-wait) is to spawn a new thread, and issue a loop of outputting "Hello World" and sleeping for 3 seconds.

Yarel
  • 424
  • 1
  • 6
  • 15