C++ Logo


Advanced search

Re: Remove infinite loop UB

From: connor horman <chorman64_at_[hidden]>
Date: Fri, 8 May 2020 23:03:22 -0400
On Fri, May 8, 2020 at 22:46 Thiago Macieira via Std-Proposals <
std-proposals_at_[hidden]> wrote:

> On Friday, 8 May 2020 18:02:59 PDT connor horman via Std-Proposals wrote:
> > Currently in the C++ language, it is undefined behaviour to have an
> > infinite loop without observable side effects. While this is nice on
> paper,
> > it has a few issues. Any time you need to loop forever, you basically
> need
> > to insert a (potentially costly) side effect.
> Any time? How often do you need to do that? The only case I can think of
> is of
> an OS that has finished running and doesn't know how to turn the device
> off.
> Can you give an example different from the above?

SNES Game that's finished but needs still draw/redraw stuff. STP will mask
the NMI from the VBLANK signal, which is how the game knows to load stuff
into VRAM on the PPU, so it can be sent out on the multiout connector when
the VBLANK is done.

> > A further case would be an init process on linux, which cannot terminate
> > (doing so results in a kernel panic), nor can just call hlt in a loop, as
> > that's one way to get a #GP which probably translates to SIGILL.
> It should call reboot(2). This is not a valid example.

Not if the OS is still running it shouldn't.

> > Once an
> > init process is done setting up, it could want to just spin forever, and
> do
> > so using as little system resources as possible. The logical idea would
> be
> > just for(;;) std::this_thread::yield();, but that would be UB as yield()
> is
> > not observable (correct me if I'm wrong).
> It can use less resources by calling a function that will sleep forever,
> like
> poll() with no timeout and no file descriptors. In any case, I don't know
> for
> sure, but I think yield() counts as observable side-effect.

It would still have to be in a loop to prevent spurious wake ups.
This was also just an example of code that may want to spin forever in
userspace, and can't just hlt.

> > Having the ability to spin forever, without wasting time actually doing
> > stuff, seems like a reasonable thing to have in low-level code
> Yes, in low-level code. Assembly.

"There is no lower-level language" was one of my main points, and a core
design principle of C++ if I remember correctly,p.

> --
> Thiago Macieira - thiago (AT) macieira.info - thiago (AT) kde.org
> Software Architect - Intel System Software Products
> --
> Std-Proposals mailing list
> Std-Proposals_at_[hidden]
> https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals

Received on 2020-05-08 22:06:34