Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to understand the new feature cooperative program Coroutines of Central20

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly explains "how to understand the new features of Category 20 Coroutines". The content of the explanation is simple and clear, and it is easy to learn and understand. please follow the editor's train of thought to study and learn "how to understand the new features of Clover 20 Coroutines".

Catalogue

1 、 co_await

2. The use of three interfaces of awaiter

3. Review of the usage of the cooperative process.

For those of you who want to know the content of the previous article, please click on the feature collaboration program Coroutines (1).

Talk about what a cooperative journey is. The functions of co_yield and co_return are also introduced. This article introduces co_await.

1 、 co_await

One is shaped like:

Co_await awaitable

The expression of is called an await-expression. The co_await expression is used to pause the running of the current protocol and wait for the result of awaitable. Awaitable then calculates and finally returns an awaiter structure to tell co_await what to do.

The function block in which co_await is located is itself a co-program, so the co_await must also be equipped with a promise and a coroutine_handle. Just like the generator class and so on in the last article.

This awaitable can be a lot of things. First, it will check whether promise provides await_transform function. If it does, it will use it. No matter if not.

As long as any await_transform is provided, each awaitable needs to find a suitable overload, otherwise an error will be reported. Implementers of the library can use the await_transform interface to limit which awaitable can be used in the collaboration process. See https://stackoverflow.com/q/65787797/14406396)

After that, the function operator co_await is looked up and the operator is expected to return an awaiter. It is already an awaiter.

2. The use of three interfaces of awaiter

An awaiter needs to implement three interfaces await_ready (), await_suspend (std::coroutine_handle)

), await_resume ()

As long as the thing that implements these three interfaces is awaiter.

Await_ready () tells co_await whether he is ready or not.

Await_suspend (h) can optionally return void, bool, std::coroutine_handle

One. H is the handle of this protocol. P is the promise type of this protocol (or void, see the explanation in the third article).

If await_ready () returns false, the collaboration will be paused. After that:

If the return type of await_suspend (h) is std::coroutine_handle, then the handle will be restored. That is, run await_suspend (h). Resume (). This means that when this protocol is suspended, another collaboration can be resumed.

If the return type of await_suspend (h) is bool, then if you look at the result of await_suspend (h), false will restore itself.

If the await_suspend (h) return type is void, then execute it directly. Suspend this agreement after execution.

If await_ready () returns true or the co-program is restored, then await_resume () is executed, and the result is the final result.

So, the await_ready, await_suspend and await_resume interfaces represent "ready", "non-stop" and "what to do" respectively. The design is natural.

The cooperative program of C++ is asymmetric and has a called / called relationship. If a co-program is awakened by something, the next time it pauses, it will return the control flow to the thing that awakens it. Therefore, the cooperative program of C++ can be regarded as a reentrant function.

3. Review of the usage of the cooperative process.

Let's take a look at the pseudo code in the previous article.

{promise-type promise (promise-constructor-arguments); try {co_await promise.initial_suspend (); / / pause function-body / / function body} catch (...) {if (! initial-await-resume-called) throw; promise.unhandled_exception ();} final-suspend:co_await promise.final_suspend (); / / Last pause}

What appears in the catch block! initial-await-resume-called refers to whether the await_resume () returned by promise.initial_suspend () has ever been executed.

If executed, the flag will immediately become true. Then call promise.unhandled_exception () to handle the exception.

An example:

Because co_await has no restrictions on what these three things should do, it can be used to implement many functions.

For example (from the standard library), for example, if we want to design a collaborative program that can stop for any positive length of time, we can design it like this:

Template auto operator co_await (std::chrono::duration) / / operator co_await {struct awaiter {std::chrono::system_clock::duration duration; awaiter (std::chrono::system_clock::duration d): duration (d) {} bool await_ready () const {return duration.count ()

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report