[cpp-threads] infinite loops

Doug Lea dl at cs.oswego.edu
Mon Jul 31 14:46:30 BST 2006


Herb Sutter wrote:
> 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,

No. Sorry not to be clear. The ints here are just a device for
illustration. Same idea applies to pointer-chasing loops etc.
Normally, you'd choose k==1, i.e., that loop executes once (if zero times
then it is uninteresting). But maybe you can only prove that it loops twice,
and that in this case the reordering is OK. Letting k vary like this
was just an attempt to allow the basic optimization Hans has in mind to
essentially always apply.


> and does the
> "you" in "you can establish" mean the compiler or some static analysis?

Yes; compilers by virtue of performing some static analysis of the sort
they'd normally do with loops anyway.

-Doug




More information about the cpp-threads mailing list