Chap 4 Flashcards

1
Q

Does async start a new thread?

A

It’s up to the implementation or wheter the task runs synchronously

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Give an example of async that runs a new thread

A

Auto f6=async (launch::async, Y(), 1.2)
Which calls tmpy(1.2), where tmpy is the move constructor from Y()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Give an example of function call deferred until either wait() or get() are called on the future

A

Auto f7= ( launch::deffered, baz, ref(x)) baz(x) will be called either when
f7.get()

f7.wait()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

When do you use async?

A

You use it to start an asynchronous task for which you don’t need the result right away.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What does async return?

A

Async returns a future object.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is a future?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Difference between future and share_future

A

Future (unique) is the only instances that refers to its associated event. Multiple instances of shared_future can refer to the same event

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What does packaged_task do?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What is the template parameter for the packaged_task<> class template?

A

Is a function signature

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What do you need to pass when you construct a packaged_task<>?

A

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))

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What’s the goal of promise<T></T>

A

Promise<T> provides a means of setting a value of type T that can be later read through an associated future<T></T></T>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What would a promise/future pair accomplish

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

How do you set the value of the promise?

A

Using the set_value() member function, then the future becomes ready

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What happens if you destroy the promise without setting the value?

A

An exception will be stored instead

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What happens with an exception returned by the function call from async/packaged_task?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

How does promise provide exception facility?

A

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

17
Q

How can you store an exception in a future?

A

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

18
Q

Difference between future and shared_future?

A

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

19
Q

Types of timeout

A

Duration based (wait_for 30 secs) or absolute (wait_until 20 dec 2024)

20
Q

What’s the return type of wait_for and wait_until?

A

Future_status enum class, with values ready, timeout, deferred

21
Q

What are the 2 params for launching an async task?

A

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)

22
Q

What facilities can take a timeout?

A

Sleeping, condition variables and futures. Some mutexes, timed_mutex

23
Q

Pure functions

A

Called twice with the same params, same result, doesn’t modify shared data