mirror of
https://github.com/boostorg/cobalt.git
synced 2026-01-19 04:02:16 +00:00
26 lines
872 B
Plaintext
26 lines
872 B
Plaintext
== delay op
|
|
|
|
We've used the `use_op` so far, to use an implicit operation based on asio's completion token mechanic.
|
|
|
|
We can however implement our own ops, that can also utilize the `await_ready` optimization.
|
|
Unlike immediate completion, the coroutine will never suspend when `await_ready` returns true.
|
|
|
|
To leverage this coroutine feature, `cobalt` provides an easy way to create a skipable operation:
|
|
|
|
.example/delay_op.cpp
|
|
[example]
|
|
[source,cpp]
|
|
----
|
|
include::../../example/delay_op.cpp[tag=timer_example]
|
|
----
|
|
<1> Declare the op. We inherit `op` to make it awaitable.
|
|
<2> The pre-suspend check is implemented here
|
|
<3> Do the wait if we need to
|
|
<4> Use the <<op, op>> just like any other awaitable.
|
|
|
|
This way we can minimize the amounts of coroutine suspensions.
|
|
|
|
While the above is used with asio, you can also use these handlers
|
|
with any other callback based code.
|
|
|