Topic: Does std::launch::sync permit speculative execution?


Author: Anthony Williams <anthony.ajw@gmail.com>
Date: Thu, 19 Nov 2009 17:17:41 CST
Raw View
Scott Meyers <usenet@aristeia.com> writes:

> When std::async is invoked with a std::launch::sync policy, must the
> invocation of the async function be deferred until a wait or get?
> That is, given
>
> auto f = std::async(std::launch::sync(), f);
> ...
> f.get();
>
> must execution of f be deferred until get is called, or may f be executed
> earlier?

Execution of f must be deferred until get is called.

Use std::launch::any to allow speculative execution.

>  Or if I write
>
> auto f = std::async(std::launch::sync(), f);
> ...
> if (some condition that turns out to be false) f.get();
>
> may I assume that f was never executed (and hence none of its side
> effects took place)?

Yes.

Anthony
--
Author of C++ Concurrency in Action | http://www.manning.com/williams
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

[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@netlab.cs.rpi.edu]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]





Author: Scott Meyers <usenet@aristeia.com>
Date: Thu, 19 Nov 2009 13:31:47 CST
Raw View
When std::async is invoked with a std::launch::sync policy, must the invocation
of the async function be deferred until a wait or get?  That is, given

 auto f = std::async(std::launch::sync(), f);
 ...
 f.get();

must execution of f be deferred until get is called, or may f be executed
earlier?

N2973, which is presumably a document that was used as background for the
current specification of std::async, notes that

    Lazy evaluation permits speculative execution. Rather than wait to
invoke the
    function when the result is known to be needed, one can invoke
async earlier.
    When there are sufficient processor resources, the function executes
    concurrently and speculatively. When there are not sufficient resources, the
    function will execute only when truly needed.


Is such behavior intended to be permitted for std::async as specified
in N3000?  Or if I write

 auto f = std::async(std::launch::sync(), f);
 ...
 if (some condition that turns out to be false) f.get();

may I assume that f was never executed (and hence none of its side
effects took place)?

Thanks,

Scott




--
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@netlab.cs.rpi.edu]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]