Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In what context will resumable functions execute in C++14?

Tags:

One of the proposals for C++14 is Resumable Functions which gives C++ what is available in C# today with the async/await mechanisms. The basic idea is that a function can be paused while waiting for an asynchronous operation to complete. When the asynchronous operation completes the function can be resumed where it was paused. This is done in a non-blocking way so that the thread from which the resumable function was invoked will not be blocked.

It is not obvious to me in which context (thread) the function will be resumed. Will it be resumed by the thread from which the function was paused (this is how it is done in C# as I understand it) or does it use another thread?

If it is resumed by the thread from which it was paused, does the thread has to be put in some special state or will the scheduler handle this?

like image 974
Tobias Furuholm Avatar asked Jun 02 '13 10:06

Tobias Furuholm


1 Answers

To quote from N3564:

After suspending, a resumable function may be resumed by the scheduling logic of the runtime and will eventually complete its logic, at which point it executes a return statement (explicit or implicit) and sets the function’s result value in the placeholder.

It should thus be noted that there is an asymmetry between the function’s observed behavior from the outside (caller) and the inside: the outside perspective is that function returns a value of type future at the first suspension point, while the inside perspective is that the function returns a value of type T via a return statement, functions returning future/shared_future behaving somewhat different still.

A resumable function may continue execution on another thread after resuming following a suspension of its execution.

This essentially means that

  • When first called, a resumable function executes in the thread context of its caller.
  • After each suspension point, the implementation can freely choose on which thread to continue the execution of a resumable function
  • From the perspective of the calling code, a resumable function works like an asynchronous function, where part of the (observable) behaviour is reliably executed by the time the function call returns, but the final result might not be in yet (the returned future<T> does not have to be in a ready state).
  • As a programmer, you don't have to jump through hoops to get a resumable function to resume.
like image 152
Bart van Ingen Schenau Avatar answered Oct 20 '22 20:10

Bart van Ingen Schenau