From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from m15114.mail.126.com (m15114.mail.126.com [220.181.15.114]) by sourceware.org (Postfix) with ESMTPS id 8291F386EC42 for ; Tue, 12 Jan 2021 16:54:07 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org 8291F386EC42 Received: from [192.168.1.3] (unknown [222.67.199.108]) by smtp7 (Coremail) with SMTP id DsmowABHcyAl1P1fIufbNA--.59917S2; Wed, 13 Jan 2021 00:53:58 +0800 (CST) To: Jonathan Wakely , Florian Weimer Cc: =?UTF-8?B?4piCSm9zaCBDaGlhICjorJ3ku7vkuK0pIHZpYSBHY2MtaGVscA==?= References: <8735z6i8fn.fsf@oldenburg2.str.redhat.com> From: Liu Hao Subject: Re: Failure to optimize? Message-ID: <1129702b-f187-d8f4-3070-f0122834ffc8@126.com> Date: Wed, 13 Jan 2021 00:53:54 +0800 User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:78.0) Gecko/20100101 Thunderbird/78.6.0 MIME-Version: 1.0 In-Reply-To: Content-Type: multipart/signed; micalg=pgp-sha256; protocol="application/pgp-signature"; boundary="BvTIEZOgRaAOlaE1tRwXIbmHYfgXqYQ3p" X-CM-TRANSID: DsmowABHcyAl1P1fIufbNA--.59917S2 X-Coremail-Antispam: 1Uf129KBjvJXoW7Cryftr17Gr1ktry5AFW7XFb_yoW8Jw4DpF ZrKr98Aws09an2y34qyw1DXr1DZ3yIq348Xr95Kr10yw43CrWDJFZ2yFW5CFyrGr97tF4a qrW8Zry3AFW5XaDanT9S1TB71UUUUUUqnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDUYxBIdaVFxhVjvjDU0xZFpf9x07jxgArUUUUU= X-Originating-IP: [222.67.199.108] X-CM-SenderInfo: 5okbz0xxvhqiyswou0bp/1tbi2QYYRlpEAw4TSwAAsK X-Spam-Status: No, score=-3125.1 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_FROM, NICE_REPLY_A, RCVD_IN_BARRACUDACENTRAL, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=no autolearn_force=no version=3.4.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on server2.sourceware.org X-BeenThere: gcc-help@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-help mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 12 Jan 2021 16:54:10 -0000 This is an OpenPGP/MIME signed message (RFC 4880 and 3156) --BvTIEZOgRaAOlaE1tRwXIbmHYfgXqYQ3p Content-Type: multipart/mixed; boundary="tgo0gK0uIACMRIGOopYpwTrcxyBv3RvL1"; protected-headers="v1" From: Liu Hao To: Jonathan Wakely , Florian Weimer Cc: =?UTF-8?B?4piCSm9zaCBDaGlhICjorJ3ku7vkuK0pIHZpYSBHY2MtaGVscA==?= Message-ID: <1129702b-f187-d8f4-3070-f0122834ffc8@126.com> Subject: Re: Failure to optimize? References: <8735z6i8fn.fsf@oldenburg2.str.redhat.com> In-Reply-To: --tgo0gK0uIACMRIGOopYpwTrcxyBv3RvL1 Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US Content-Transfer-Encoding: quoted-printable =E5=9C=A8 2021-01-12 22:20, Jonathan Wakely via Gcc-help =E5=86=99=E9=81=93= : >=20 > I'm not sure about the rules for C, but in C++ the compiler can assume > there is no race, because the increment is not atomic. If there were > another access to the variable then a non-atomic store would be a race > even in the bar1 version. >=20 What about this code: // -- beginning of copy-n-pasted code char const* foo(); int cursor =3D 0; char const* bar1() { char const* result =3D foo(); if (result) ++cursor; return result; } char const* bar2() { char const* result =3D foo(); cursor +=3D !!result; return result; } // -- end of copy-n-pasted code #include #include #include char const* foo() { static ::std::atomic str("meow"); return str.exchange(nullptr); } int main() { ::std::thread thrs[10]; for(auto& r : thrs) r =3D ::std::thread(bar1); for(auto& r : thrs) r.join(); ::std::printf("cursor =3D %d\n", cursor); } `foo()` will return non-null for exactly one thread. Increment of `cursor= ` by that thread is=20 sequenced before its termination, which synchronizes with exactly one `jo= in()`, which is sequenced=20 before the final read of `cursor`. There is no race in this program, but = there would be if `bar2`=20 was called in place of `bar1`, where all threads could modify `cursor` co= ncurrently. --=20 Best regards, LH_Mouse --tgo0gK0uIACMRIGOopYpwTrcxyBv3RvL1-- --BvTIEZOgRaAOlaE1tRwXIbmHYfgXqYQ3p Content-Type: application/pgp-signature; name="OpenPGP_signature.asc" Content-Description: OpenPGP digital signature Content-Disposition: attachment; filename="OpenPGP_signature" -----BEGIN PGP SIGNATURE----- wsF5BAABCAAjFiEEn9eDGptREvfrWRePQbMyeCIxW8QFAl/91CIFAwAAAAAACgkQQbMyeCIxW8Ro JA/+LEq6KCTMjjkTOR5HckwFtw7YxiJ70dvNI3j9wdqo+Kx5pv8Cc4t9ma3xjbUfh/zgYmAxdLcm m9CNPSf6YO5BzWPycypMb5GLsrDoM8VIGJ1fH/H6YYoah4IOezH5cvMQLPE9wsXowx9VXpyBl4XT BEbNI5TmdzB30exMQ3SOUhtLd4b01vng0fAvC0SzYQ2JdQQ1khvWr4N+KaUDjFkh+KKQqrlFd0y5 kVolqtuqXyzcNfXJU0/FYci0AstwNU4xO6FnUxQUUuvQtcK0rYtOhqoghqqDCSxW0n2eutWhC8o3 KUKgIWNoHL2FHnArfHA2lMOEccP28KQvSL91eY34nopMnzRy0bnrws7GpEs+GZ7w/z19Wq5Fe2pw FfswYmtlfHfvZekqfLEzyQDutl17R0uEGe+NzHDz9vELdEsZvD8U1YzR/k8caZWAAUJ3Cj4fCIcf eNvUHaG4JPR0BaxdYe1nr0C7X53drljkhUpZh4R4K8lR2lUsqTsHczF2ldWHklrBPxwq44c0uc9R VV4Gv5b+JHaWkjXLxqREGSb1aUX5CLLEZH6aF42+dFjMKzaoUF0hnQwAXZHGPnqDyJpBI/TFFBsx 7+ra44LFPqoTAM3Kck41HC+OPulgqXwRpSumBHkhP5EpjrHNP3wJSveKnM2TDtKhSUdHk5zt/RNO cHc= =sf+w -----END PGP SIGNATURE----- --BvTIEZOgRaAOlaE1tRwXIbmHYfgXqYQ3p--