-
Notifications
You must be signed in to change notification settings - Fork 95
3.2 Returning from a task
Both coroutines and IO tasks have similar signatures as both return an integer. This is the typical unix-style return code (0 on success, != 0 otherwise) which signals the scheduler if the task completed successfully or not. Note that this has nothing to do with the promise/future return type which we will see later.
Example of success and error return values:
dispatcher.post([](CoroContextPtr<char> ctx)->int
{
ctx->yield(); //Yield point is executed. Coroutine exists but it not terminated
if (some_error_condition)
{
return -1; //Error. Signals coroutine termination
}
ctx->yield(); //Never executes
return 0; //Never executes
});
It is worth mentioning that when a return statement is encountered in a coroutine, this coroutine is terminated and removed from the thread queue. Even if there are other yield points following the return statement, they will not be executed.
All uncaught exceptions which are thrown from inside a coroutine or an IO task are automatically caught by the scheduler and not propagated further. This will prevent the executing thread from being terminated. After the exception is caught, the next coroutine or IO task is scheduled to run.
Sometimes however it is necessary to inform the caller (which posted the coroutine or IO task) that an exception has occurred or even transfer this exception back. This mechanism called "exception marshaling" is permitted via the promise/future handshake in which the caught exception is simply funneled into the future object the caller waits upon. When this happens, the caller thread wakes up and the exception is re-thrown. See the section on futures and promises for an example.