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)
How does promise provide exception facility?
Explicit function call set_exception instead of set_value, in a catch block, this will throw. The alternative is to use make_exception_ptr, that will not throw
How can you store an exception in a future?
Destroy the promise or associated packaged_task without calling either of the set functions or invoking the packaged_task. The destructors will store future_error exception if the future isnt already ready
Difference between future and shared_future?
Future models unique_ownership of the asynchronous result, while shared_future allows that multiple threads wait for the same result. Future is moveable, whereas shared_future is copyable
Types of timeout
Duration based (wait_for 30 secs) or absolute (wait_until 20 dec 2024)
What’s the return type of wait_for and wait_until?
Future_status enum class, with values ready, timeout, deferred
What are the 2 params for launching an async task?
Launch async (task executed on a diff thread, potentially by creating and launching it first) or launch deferred(task executed on the calling thread the first time is requested)
What facilities can take a timeout?
Sleeping, condition variables and futures. Some mutexes, timed_mutex
Pure functions
Called twice with the same params, same result, doesn’t modify shared data