How do I create a timer? A timer like the one in Visual Basic; you set an interval, if the timer is enabled it waits until the time is up.
I don't want to use an existing library because I want to know how it works.
So.. I just hope someone could explain me how timers work and maybe give me an example of code to create my own - if it's not too advanced.
Edit: I wanna create one for a linux system.
The following is a very basic example which will run under Linux. If you look at the manual page of
signal
you will see it is deprecated in favor ofsigaction
. Important is not to forget thevolatile
, otherwise thewhile
loop may not terminate depending on optimizations. Note also howSIGALRM
is a highly shared resource which may be used by other timer facilities and there is only one.The program will print
Waiting
for three seconds and than quit after printingFinally ...
once.You can do that