C++ C++20 coroutines IPC Linux
Let’s start with practically zero knowledge of the concept of 'coroutines
‘ in C++20. It’s time to learn the basics and dig a little deeper to understand what it entails.
Let’s start with virtually zero knowledge of the concept of ‘coroutines’ in C++20. It’s time to learn the basics and delve a little deeper to understand what this entails. The first place to look is, of course, cppreference.
CPP Reference
Coroutines (C++20)
A coroutine is a function that can suspend execution to be resumed later.
Ok, so at this point I understand that ‘coroutine’ can be treated as a standard function. The difference is that a standard function is executed from start to finish, while here we are dealing with the possibility to pause the function and jump to the execution of another piece of code. And I guess it will be possible to continue later.
Coroutines (C++20)
Coroutines are stackless: they suspend execution by returning to the caller and the data that is required to resume execution is stored separately from the stack.
Another difference between ‘coroutines’ and a standard function. The whole procedure is not kept on the stack. This makes a lot of sense due to having the ability to invoke this function at any point in the programme and continue a stopped operation.
Coroutines (C++20)
This allows for sequential code that executes asynchronously (e.g. to handle non-blocking I/O without explicit callbacks), and also supports algorithms on lazy-computed infinite sequences and other uses.
It allows ‘asynchronous’ operations, nevertheless it is only executed in a single thread. Typical applications:
- operation on data streams
- lazy computations
When function becomes coroutines?
If function definition contains any of the following:
- co_await
- co_yield
- co_return
When function can NOT become coroutines?
Coroutine can not use any of the following:
- variadic arguments
- plain ‘return’
- ‘auto’ or ‘concept’ as placeholder return type
What is each coroutine associated with?
Promise
In addition, ‘coroutine’ is linked to the ‘promise’ object. As I understand it, we are dealing with a container which is modified inside the function and through which the result of the function can be sent or an exception can be thrown which can be handled accordingly outside.
Coroutine handle
Just like a standard handle, it is used to operate the function. In this case, it can be used to resume the function or to delete it completely. Importantly, this handle is not owning.
Coroutine state
An additional object associated with a function that stores its (current) state. Unless this object has been optimised, it is stored in dynamically allocated memory. The information that is stored includes:
- ‘promise’ object
- copies of parameters (by value) – here I am not sure, but I am guessing that it is a copy of the function parameters.
- ‘suspension point’ information
- local function variables that were created from the start of the function until the ‘suspension point’ was reached
What happen when coroutine starts?
Allokowany jest
Differences
Standard function | Coroutines |
---|---|
Stored on stack | Can not live in stack. I guess in most implementations that will be heap, where it lives. |
Runs from the beginning to ‘return’ or ‘}‘. | Can be ‘paused’ and ‘resumed’. |