Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.programming.threads > #1794
| Path | csiph.com!eeepc.pasdenom.info!news.pasdenom.info!news.dougwise.org!nntpfeed.proxad.net!proxad.net!feeder1-2.proxad.net!weretis.net!feeder4.news.weretis.net!news1.tnib.de!feed.news.tnib.de!news.tnib.de!feed.cnntp.org!news.cnntp.org!not-for-mail |
|---|---|
| From | Anthony Williams <anthony.ajw@gmail.com> |
| Newsgroups | comp.programming.threads |
| Subject | Re: forcing the compiler to reload from memory with c++0x |
| References | <4d3cee42$0$1209$426a74cc@news.free.fr> <1d21ad0a-db55-460e-aeb6-66f6d19369ea@i13g2000yqe.googlegroups.com> <4d3e2e3f$0$21517$426a34cc@news.free.fr> <4D40A4C7.16E6F9AE@web.de> <69d82db7-34be-4de7-86c4-2f39cc1e6df3@m13g2000yqb.googlegroups.com> <4d4221e9$0$17247$426a74cc@news.free.fr> <947b33d5-2509-4f0c-9256-a7c3de35578e@24g2000yqa.googlegroups.com> <87mxmlxxry.fsf@justsoftwaresolutions.co.uk> <ihv8t6$bmr$1@news.eternal-september.org> |
| Date | Sun, 30 Jan 2011 21:58:55 +0000 |
| Message-ID | <87oc6yxdcw.fsf@justsoftwaresolutions.co.uk> (permalink) |
| User-Agent | Gnus/5.13 (Gnus v5.13) Emacs/23.1 (gnu/linux) |
| Cancel-Lock | sha1:XMdNE0uRSJzDfydbty8yZEPA/ds= |
| MIME-Version | 1.0 |
| Content-Type | text/plain; charset=us-ascii |
| Lines | 67 |
| Organization | CNNTP |
| NNTP-Posting-Host | 1dbafa7a.read.cnntp.org |
| X-Trace | DXC=dmn3b9hXONi[:`?W2<9:HiWoT\PAgXa?a1LKeO=ej5Zh31?8ZVJ3gMeGj6PdB418WniDWEWZmNi6ba8\JCd=f3La |
| X-Complaints-To | abuse@cnntp.org |
| Xref | csiph.com comp.programming.threads:1794 |
Show key headers only | View raw
Andy Venikov <swojchelowek@gmail.com> writes:
> On 1/28/2011 3:01 AM, Anthony Williams wrote:
>>
>> If you are only using your fence to prevent compiler reordering then you
>> can use atomic_signal_fence rather than atomic_thread_fence. The former
>> is primarily a compiler directive, and will likely not issue any CPU
>> instructions.
>
> Sorry to be asking too many "stupid" questions, but this area is
> important to understand.
>
> So, if I have
>
> atomic<int> a_int;
> ...
> while (!a_int.get(relaxed))
> {
> }
>
> The compiler is allowed to optimize the while loop away?
Yes, but with caveats.
There are no guarantees about how quickly a given thread runs, so it
could be suspended for arbitrary amounts of time at arbitrary points.
If the value of a_int is ever set to non-zero then the compiler is free
to omit the loop, since relaxed operations can read future values, so it
can assume that it read the non-zero future value. It has to be careful
not to invalidate modifcation orders, though --- future reads of a_int
from the same thread must read either the non-zero value that meant the
loop was omitted, or a later value in the modification order of a_int.
This could be achieved by having a_int.get() block until it knew what
that non-zero value was.
If the compiler does not know if a_int is ever set to non-zero, then it
must schedule at least one read of a_int. If it enters the loop, then it
must perform further reads of a_int as required by memory ordering
constraints on other variables. If there are no such constraints, then
it only need issue another read in some finite amount of time (which
could be large).
Such things are entirely QoI: in practice, the compiler will emit a real
read which is run each loop iteration.
> Will using atomic_signal_fence (or atomic_thread_fence for that
> matter) force the compiler to emit a code that will do busy-waiting?
In practice, yes. In principle, if the compiler knows there will be no
signals handled on this thread then atomic_signal_fence imposes no
restrictions.
Compilers try to work with programmers, not against them, so they won't
optimize out such a loop unless they have good reason to believe it
won't execute, such as
a_int.store(1);
while(!a_int.load()){ do_stuff(); } // can be omitted.
Anthony
--
Author of C++ Concurrency in Action http://www.stdthread.co.uk/book/
just::thread C++0x thread library http://www.stdthread.co.uk
Just Software Solutions Ltd http://www.justsoftwaresolutions.co.uk
15 Carrallack Mews, St Just, Cornwall, TR19 7UL, UK. Company No. 5478976
Back to comp.programming.threads | Previous | Next | Find similar | Unroll thread
Re: forcing the compiler to reload from memory with c++0x Anthony Williams <anthony.ajw@gmail.com> - 2011-01-30 21:58 +0000
csiph-web