firstworks Programming with Rudiments using the snooze class

Using the snooze class

The snooze class provides static methods for suspending process execution.

Processes may be suspended for times specified in second, microsecond or nanosecond granularity. Methods for snoozing come in two varieties: snoozes which return when interruped, and snoozes which automatically resume when interrupted. The methods which return when interrupted also return the time that remained at the point that they were interrupted.

> #include <rudiments/snooze.h>
> #include <rudiments/stdio.h>
> int main(int argc, const char **argv) {
> // declare some variables that we'll use later
> uint32_t remainingseconds;
> uint32_t remainingmicroseconds;
> uint32_t remainingnanoseconds;
> // snooze for 2 seconds, auto-recovering from interruptions
> stdoutput.write("snoozing for 2 seconds...\n");
> snooze::macrosnooze(2);
> // snooze for 2 seconds, allowing interruptions
> stdoutput.write("snoozing for 2 seconds (interruptable)...\n");
> if (!snooze::macrosnooze(2,&remainingseconds)) {
> stdoutput.printf("interrupted... %d seconds remained\n",
> remainingseconds);
> }
> // snooze for 200 microseconds, auto-recovering from interruptions
> stdoutput.write("snoozing for 200 microseconds...\n");
> snooze::microsnooze(0,200);
> // snooze for 200 microseconds, allowing interruptions
> stdoutput.write("snoozing for 200 microseconds (interruptable)...\n");
> if (!snooze::microsnooze(0,200,
> &remainingseconds,
> &remainingmicroseconds)) {
> stdoutput.printf("interrupted... %d seconds and "
> "%d microseconds remained\n",
> remainingseconds,
> remainingmicroseconds);
> }
> // snooze for 200 nanoseconds, auto-recovering from interruptions
> stdoutput.write("snoozing for 200 nanoseconds...\n");
> snooze::nanosnooze(0,200);
> // snooze for 200 nanoseconds, allowing interruptions
> stdoutput.write("snoozing for 200 nanoseconds (interruptable)...\n");
> if (!snooze::nanosnooze(0,200,
> &remainingseconds,
> &remainingnanoseconds)) {
> stdoutput.printf("interrupted... %d seconds and "
> "%d nanoseconds remained\n",
> remainingseconds,
> remainingnanoseconds);
> }
> }

It is safe to pass NULL in for the secondsremaining, microsecondsremaining, and nanosecondsremaining parameters. This is useful if you want the snooze to be interruptable, but you don't care about the amount of time remaining after it was interrupted.