I am aware that there are available functions and libraries that allow concurrency in c.
(pthread.h, fork() etc.)
However, I was wondering if there is any way to simulate concurrency in C, without actually having to use more than one thread?
Example scenario:
There is a main program loop running and outputting calculations. Somehow another function notifies the main function that the time is now 12:00 p.m. and the program should stop outputting calculations as the user is going off to lunch. At 12:30 this function notifies the main function to start outputting calculations again.
Can someone point me in the right direction on how to do this?
EDIT:
In essence I believe there should be 2 ways to do this:
The first would be the main program constantly checking the alternate function so it knows when the clock hits 12:00. (this is very simple and I know how to do this)
The second would be having the alternate function contact the main program whenever required (i.e. at 12:00 and 12:30).
It seems to me like you just need a simple timer you could use
alarm()for that:If however, you want to have something like coroutines you should look into user-level threads (or fibers) using functions like
makecontext()andswapcontext()Note: this example is UNIX/Linux specific, for MinGW you will need to use winapi, using functions like
CreateWaitableTimer(),SetWaitableTimer(), and so on. See MSDN for details.