[cpp-threads] infinite loops

Herb Sutter hsutter at microsoft.com
Mon Jul 31 04:36:34 BST 2006


Doug Lea wrote:
> Boehm, Hans wrote:
> > A way out of this dilemma appears to be to state that
> >
> > ("infinite loop restriction") Programs that contain infinite loops
> > without "acquire+" operations have undefined behavior.
> 
> Would it suffice to instead say something (not sure how)
> that amounts to the following, to avoid undefinedness?
> 
> A construction of the form
>    for (i=0;    ; ++i) something; x = 42;
> can be transformed to
>    x = 42; for (i=0;    ; ++i) something;
> if you can establish some k > 0 such that
>    for (i=0; i<k; ++i) something; x = 42;
> can be transformed to:
>    x = 42; for (i=0; i<k; ++i) something;

Do you mean to restrict it to such integer loop variables, and does the
"you" in "you can establish" mean the compiler or some static analysis?

In general we also want to cover the general case of loops doing
arbitrary work (including pointer-chasing loops), many which can be
infinite based on run-time data and therefore whether they terminate is
statically unknowable. Canonical example:

  for( p = head; p != null; p = p->next ) something; x = 42;

Even if the list contains a loop and so the for loop goes infinite, it
could be desirable to permit the memory model to reorder the assignment
to x.

Herb




More information about the cpp-threads mailing list