logoalt Hacker News

rjp0008last Friday at 8:29 PM3 repliesview on HN

What if in the time between initialization of cosmic_ray to False, and the time this if statement executes, a legitimate cosmic ray flips the bool bit representing cosmic_ray?


Replies

sunrunnerlast Friday at 8:48 PM

This is a really good point and a common error in bit flip detection code. To avoid this kind of look-before-you-leap hazard the following is recommended:

    try {
        do_action()
    } catch (BitFlipError e) {
        logger.critical("Shouldn't get here")
    }
Ask-for-forgiveness as an error detection pattern avoids these kinds of errors entirely.
terminalshortlast Saturday at 1:40 AM

Simple! Make it an int.

  int cosmic_ray = 0
  if (bool(cosmic_ray)) {
     throw cosmicRayException()
  }
wavemodelast Friday at 8:45 PM

ah, a classic TORTOF bug (time-of-ray, time-of-flip)