C++ Logo

sg5

Advanced search

Re: [SG5] P2066R8: atomic do while

From: Jens Maurer <Jens.Maurer_at_[hidden]>
Date: Sat, 4 Sep 2021 10:32:40 +0200
On 03/09/2021 03.24, Joe Gottman via SG5 wrote:
> I have a question about the new atomic do statement from P2066R8. What
> happens if a user attempts to combine this with a do-while loop? Suppose
> someone wrote code like the following:
>
> extern int i; // Starts off equal to 0
>
> void f() {
> atomic do {
> ++i;
> } while (i < 10);
> }
>
> I have 2 questions. Would this code compile,

Yes, I think so.

> and if so what would it do?

atomic do {
  ++i;
}

while (i < 10)
  ; // likely an endless loop


> I think it would be the same as an atomic do followed by an empty while
> loop, and thus result in an infinite loop. It might be a good idea to
> change the atomic do statement to require a semicolon after the closing
> bracket, the way a semicolon is required at the end of a regular
> do-while loop.

C++ does not require a semicolon after a compound-statement, anywhere.
Requiring one here seems novel.

Jens

Received on 2021-09-04 03:32:50