diff --git a/coro-introduction.html b/coro-introduction.html index bc802606fb6..916b35a81a2 100644 --- a/coro-introduction.html +++ b/coro-introduction.html @@ -155,7 +155,8 @@

When using a co_* function such as co_message_create, the request is sent immediately and the returned dpp::async can be co_await-ed, at which point the coroutine suspends (pauses) and returns back to its caller; in other words, the program is free to go and do other things while the data is being retrieved and D++ will resume your coroutine when it has the data you need, which will be returned from the co_await expression.

Warning
As a rule of thumb when making coroutines, always prefer taking parameters by value and avoid lambda capture! See below for an explanation.

You may hear that coroutines are "writing async code as if it was sync", while this is sort of correct, it may limit your understanding and especially the dangers of coroutines. I find they are best thought of as a shortcut for a state machine, if you've ever written one, you know what this means. Think of the lambda as its constructor, in which captures are variable parameters. Think of the parameters passed to your lambda as data members in your state machine. When you co_await something, the state machine's function exits, the program goes back to the caller, at this point the calling function may return. References are kept as references in the state machine, which means by the time the state machine is resumed, the reference may be dangling: this is not good!

-

Another way to think of them is just like callbacks but keeping the current scope intact. In fact this is exactly what it is, the co_* functions call the normal API calls, with a callback that resumes the coroutine, in the callback thread. This means you cannot rely on thread_local variables and need to keep in mind concurrency issues with global states, as your coroutine will be resumed in another thread than the one it started on.

+

Another way to think of them is just like callbacks but keeping the current scope intact. In fact this is exactly what it is, the co_* functions call the normal API calls, with a callback that resumes the coroutine, in the callback thread. This means you cannot rely on thread_local variables and need to keep in mind concurrency issues with global states, as your coroutine will be resumed in another thread than the one it started on.

+

It is also worth noting that coroutines can lead to cases where errors may fall through and remain uncaptured, requiring the user to manually handle the error. These exceptions occur because callbacks cannot be used in their typical manner, which can be observed in the following issue and the sample coroutine code.

diff --git a/xml/coro-introduction.xml b/xml/coro-introduction.xml index 8b7e4f5a749..c71cb69aa87 100644 --- a/xml/coro-introduction.xml +++ b/xml/coro-introduction.xml @@ -50,7 +50,8 @@ As a rule of thumb when making coroutines, always prefer taking parameters by value and avoid lambda capture! See below for an explanation. You may hear that coroutines are "writing async code as if it was sync", while this is sort of correct, it may limit your understanding and especially the dangers of coroutines. I find they are best thought of as a shortcut for a state machine, if you've ever written one, you know what this means. Think of the lambda as its constructor, in which captures are variable parameters. Think of the parameters passed to your lambda as data members in your state machine. When you co_await something, the state machine's function exits, the program goes back to the caller, at this point the calling function may return. References are kept as references in the state machine, which means by the time the state machine is resumed, the reference may be dangling: this is not good! -Another way to think of them is just like callbacks but keeping the current scope intact. In fact this is exactly what it is, the co_* functions call the normal API calls, with a callback that resumes the coroutine, in the callback thread. This means you cannot rely on thread_local variables and need to keep in mind concurrency issues with global states, as your coroutine will be resumed in another thread than the one it started on. +Another way to think of them is just like callbacks but keeping the current scope intact. In fact this is exactly what it is, the co_* functions call the normal API calls, with a callback that resumes the coroutine, in the callback thread. This means you cannot rely on thread_local variables and need to keep in mind concurrency issues with global states, as your coroutine will be resumed in another thread than the one it started on. +It is also worth noting that coroutines can lead to cases where errors may fall through and remain uncaptured, requiring the user to manually handle the error. These exceptions occur because callbacks cannot be used in their typical manner, which can be observed in the following issue and the sample coroutine code.