volatile (in comp.lang.c)

T. William Wells bill at proxftl.UUCP
Thu May 19 01:11:37 AEST 1988


In article <5367 at bloom-beacon.MIT.EDU>, peter at athena.mit.edu (Peter J Desnoyers) writes:
> The debate over |volatile| seems to focus on one point: if this
> keyword is only necessary for non-portable code, why does it have to
> be part of the language?

Volatile is useful in portable code.  Here is the skeleton of one
way to use it.

#include <signal.h>

volatile sig_atomic_t Sighappened;      /* Must be THIS type. */

void
sighandler(
int signum
) {
	/* Ignore SIGINT until the previous one has been
	   processed.  Unfortunately, between the time of the
	   signal and this call a SIGINT may cause the default
	   action to be taken (this is implementation
	   dependent).  Grrrrr.  */

	signal(SIGINT, SIG_IGN);
	Sighappened = 1;
}

main()
{
	signal(SIGINT, sighandler);
	while (1) {
		if (Sighappened) {
			... handle the signal

			/* Permit SIGINT to be recognized again. */

			Sighappened = 0;
			signal(SIGINT, sighandler);
			continue;
		}
		... do something else
	}
}

(Pardon if this code has trivial flaws; I do not have easy access
to an ANSI-ish compiler.)



More information about the Comp.lang.c mailing list