Chap 4 Flashcards
Does async start a new thread?
It’s up to the implementation or wheter the task runs synchronously
Give an example of async that runs a new thread
Auto f6=async (launch::async, Y(), 1.2)
Which calls tmpy(1.2), where tmpy is the move constructor from Y()
Give an example of function call deferred until either wait() or get() are called on the future
Auto f7= ( launch::deffered, baz, ref(x)) baz(x) will be called either when
f7.get()
…
f7.wait()
When do you use async?
You use it to start an asynchronous task for which you don’t need the result right away.
What does async return?
Async returns a future object.
What is a future?
Models one off events. It can contain data or not. Once an event has happened (future is ready) it can be reset. When you need the future call get() on the future, and the thread blocks until the future is ready and returns the value
Difference between future and share_future
Future (unique) is the only instances that refers to its associated event. Multiple instances of shared_future can refer to the same event
What does packaged_task do?
Ties a future to a function or callable object. When the packaged_task is invoked, it calls the associated func or callable object, and makes the future ready, with the return value stored as the associated data
What is the template parameter for the packaged_task<> class template?
Is a function signature
What do you need to pass when you construct a packaged_task<>?
You must pass in a function or callable object that accepts the specified params and that return a type that’s convertible to the specified return type. The return type identifies the type of future<> returned from the get_future(), whereas the arg list of the funct signature specifies the signature of the packaged_task funct call operator ( void operator() (args))
What’s the goal of promise<T></T>
Promise<T> provides a means of setting a value of type T that can be later read through an associated future<T></T></T>
What would a promise/future pair accomplish
The waiting thread could block on the future, while the thread providing the data could use the promise to set the associated value and make the future ready
How do you set the value of the promise?
Using the set_value() member function, then the future becomes ready
What happens if you destroy the promise without setting the value?
An exception will be stored instead
What happens with an exception returned by the function call from async/packaged_task?
That exception is stored in the future in place of a stored value, the future becomes ready and a call to get rethrows the exception (copy or another ex)