Subject: Re: [std-proposals] Remove infinite loop UB
From: David Brown (david_at_[hidden])
Date: 2020-05-11 02:39:58
On 09/05/2020 15:18, Tony V E via Std-Proposals wrote:
> The basic idea is a program that looks like:
> int main()
> Â Â setupSignalHandlers();
> Â Â for(;;)
> Â Â Â Â ;
> The real work of the program happens in the signal handlers.
> Or interrupts. Etc.
> Nothing happens on the main thread. But it needs to be running for the
> signal handlers to have somewhere to run.Â
> We could have a special function for this:
> Â Â for (;;)
> Â Â Â std::magic_pretend_im_doing_something();
> Not sure how we specify it though.
In gcc and clang, you can use:
asm volatile ("" ::: );
It is "volatile", but does absolutely nothing.
It is standard, of course. But I can't see it being difficult to specify.
However, what you /really/ want in loops like this is something like:
while (true) sleep();
where "sleep" is highly cpu specific. Some cpu's have a "sleep"
instruction, others have a "wait for events", "halt", "HCF", etc.
This should only be used at the lowest level of the system - in the main
loop of a bare-metal embedded system, or in the idle task of an OS. Any
other task in a system with an OS should block waiting on some event, or
at the very least "yield" its time-slice.
And these sorts of things are all observable events.
I think the only use I have had for a genuinely empty infinite loop is
in handling restarts in an embedded system, where the processor is
twiddling its thumbs waiting for the watchdog to cause a restart.
STD-PROPOSALS list run by firstname.lastname@example.org
Standard Proposals Archives on Google Groups