3

I wanted to know how appropriate its to use std::async in performance oriented code. Specifically

  1. Is there any penalty in catching the exception from worker thread to main thread?
  2. How are the values returned from worker to main?
  3. Are the input arguments passed by ref actually never get copied or not?

I am planning to pass a heavy session object to a thread or write std::async.

bool fun(MySession& sessRef);
MySession sess;
auto r = std::async(&fun, sess);

EDIT:

  • I am planning to use it with GCC 4.9.1 and VS2013 both since the application is platform agnostic. However most deployments will be *nix based so atleast GCC should be performant.
Niall
  • 30,036
  • 10
  • 99
  • 142
prem.baranwal
  • 39
  • 1
  • 6
  • 1
    If you want to pass a reference, you need to [wrap it in a `reference_wrapper` with `std::ref` or `std::cref`](http://en.cppreference.com/w/cpp/utility/functional/ref). Like [`std::bind`](http://en.cppreference.com/w/cpp/utility/functional/bind) and the [`std::thread` constructor](http://en.cppreference.com/w/cpp/thread/thread/thread), [`std::async`](http://en.cppreference.com/w/cpp/thread/async) copies its arguments into other storage so their lifetime is distinct from the scope of the call. – Casey Sep 04 '14 at 19:24
  • 2
    You can implement your own explicit `async` [with a promise and a thread](http://stackoverflow.com/a/12335206/596781). – Kerrek SB Sep 04 '14 at 19:25
  • 1
    `std::async` is not guaranteed to be parallel if an `std::launch` policy of `std::launch::async` is not used. An implementation is also free to use other implementation defined values as the default and can even execute the callback eagerly. – Mgetz Sep 04 '14 at 19:31
  • 1
    You generally want to ask one question at a time, not 3. And it is a good idea to check if your question is appropriate. Implementation details will have to be compiler specific, no? – Yakk - Adam Nevraumont Sep 04 '14 at 19:56
  • @Yakk: My question was regarding Async usage in general. In case if anybody has any other input. – prem.baranwal Sep 05 '14 at 05:19

1 Answers1

2

We can't tell exactly "how is std::async implemented", since you're not referring to a particular toolchain that provides that implementation actually.

1. Is there any penalty in catching the exception from worker thread to main thread?


Define "Penalty" by which means exactly? That can't be answered unless you clarify about your concerns/requirements.
Usually there shouldn't be any penalty, by just catching the exception in the thread, that created the throwing one. It's just about the exception may be provided to the creating thread via the join(), and this causes some cost for keeping that particular exception through handling of join().

2. How are the values returned from worker to main?


To cite what's the c++ standards definition saying about this point:

30.6.8 Function template async

4 Returns: An object of type future<typename result_of<typename decay<F>::type(typename decay<Args>::type...)>::type> that refers to the shared state created by this call to async.


3. Are the input arguments passed by ref actually never get copied or not?

That point is answered in detail here: Passing arguments to std::async by reference fails. As you see, the default case they are copied.
According to @Yakk's comment, it might be possible to pass these parameters via std::ref to avoid operating on copies, but take references.


how is std::async implemented

I can tell only for the c++ standards requirements, how it should be implemented, unless you're referring to a particular toolchain, that tries to provide a proper implementation of std::async.

Community
  • 1
  • 1
πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
  • Your answer to #3 is wrong I think (they are copied, and then references to them are consumed on the other thread, unless you pass via `std::ref`). Your answer to #1 is a comment, not an answer. – Yakk - Adam Nevraumont Sep 04 '14 at 19:55
  • Currently i am using Poco::Thread and catching exception in the same thread. exceptions are kind of frequent (~1 per min) so wanted to know its performance with std::future in main thread. – prem.baranwal Sep 05 '14 at 05:19