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 comprehend the characteristic cooperation program of Category 20

2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article introduces the relevant knowledge of "how to understand the Craft 20 feature collaboration". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Catalogue

First, a brief introduction to the cooperative process

Second, the benefits of the cooperation process

Third, the usage of the cooperative process.

4. Three keywords of Xiecheng

Fifth, the working principle of cooperative process

1 、 co_yield

2 、 co_return

First of all, let's introduce what a cooperative journey is.

First, a brief introduction to the cooperative process

The co-program is actually similar to the ordinary function. However, this function can pause itself or be restored by others.

Ordinary function call, the function runs to return a value, the end.

The co-program can run halfway, return a value, and retain the context. You can continue to run the next restore, and the context (such as local variables) is still there.

This is the biggest difference.

Second, the benefits of the cooperation process

Consider the scenario of multitasking collaboration. If it's thread concurrency, then you need to grab CPU, and you need techniques such as conditional variables / semaphores or locking to ensure that the correct thread is working.

If you are in the process of cooperation, you can take the initiative to pause yourself and multiple tasks cooperate with each other. This may be a little more efficient than fighting for CPU together, because you can control which programs use CPU.

An example:

Producer / consumer model: after the producer has finished production, pause himself and return the control flow to the consumer. After consumer consumption is finished, resume producers, producers continue to produce. This cycle goes on and on.

Asynchronous invocation: for example, you want to request a resource on the network.

Send a request to the co-program

After receiving the request, the cooperative process sends out the request. The cooperative process paused itself and returned the control.

You continue to do something else. For example, make the next request. Or do some calculations.

Resume the process and get the resources (you may have to wait)

Ideally, 4 can use resources directly, so it doesn't waste time at all.

If it is synchronized:

Send a request to the function.

After receiving the request, the function waits for resources.

After waiting for a long time, when the resources arrive, give back the control.

It is obvious that we need to wait a little longer. If you need to send hundreds of requests, that's obviously the first asynchronous call that is faster. (new requests can be sent while waiting)

If there is no co-program, one solution is to use multithreading. Like this:

Send a request to the function.

Function in another thread, etc., does not block your thread.

You continue to do something else. For example, make the next request. Or do some calculations.

When he finally arrives, he will think of some way to inform you.

Then the methods of notification are promise and callback.

Third, the usage of the cooperative process.

Let's take a look at how to use the cooperative process according to the Clearing 20 standard. Test with galleys, version 10.2.

At present, the Craft 20 standard only adds the basic functions of the cooperative program, and there are no classes that can be used directly. GCC said it will try to maintain ABI compatibility with clang and MSVC and library compatibility with libc++ and others. So this article may also be applicable to them.

The communication between the co-program and the main program is carried out through promise. Promise can be understood as a pipeline that can be seen by both the co-program and its callers.

In the past, std::async and std::future also communicate based on a special kind of promise, that is, std::promise. If you want to use a co-program, you need to implement a new promise class, which is similar in principle.

4. Three keywords of Xiecheng

This time, three new keywords co_await, co_yield, co_return. In terms of effect: co_await is used to pause and resume the coordination process, and is really used to evaluate.

Co_yield is used to pause the protocol and yield a value into the bound promise.

Co_return puts a value into the bound promise.

Let's talk about co_yield and co_return. After talking about these two, it will be easier to talk about co_await.

Fifth, the working principle of cooperative process

So the two most important questions are

How to implement information transmission (using self-implemented promise)

How to resume a paused collaboration (using std::coroutine_handle)

As mentioned above, a collaborative process will have an accompanying promise, which is used as a message transmission. A cooperative process, the effect is equivalent to

{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}

Details, including promise initialization parameters, exception handling, etc., we will save for later articles. So let's simplify it to

{promise-type promise; co_await promise.initial_suspend (); function-body / / function body final-suspend:co_await promise.final_suspend ();}

For a pause, the co_await place can suspend and hand over control. In the next article, we will introduce co_await.

For Wake up, you need to get a std::coroutine_handle and call resume () on it.

1 、 co_yield

What co_yield 123 does is actually equivalent to calling co_await promise.yield_value (123). After storing 123 in this promise, you will tell co_await that you want to pause. So co_await stops here and returns the control flow.

Let's take a look at an implementation example in the standard.

# include # include struct generator {struct promise_type; using handle = std::coroutine_handle; struct promise_type {int current_value; static auto get_return_object_on_allocation_failure () {return generator {nullptr};} auto get_return_object () {return generator {handle::from_promise (* this)};} auto initial_suspend () {return std::suspend_always {} } auto final_suspend () {return std::suspend_always {};} void unhandled_exception () {std::terminate ();} void return_void () {} auto yield_value (int value) {current_value = value; return std::suspend_always {} / / this is an awaiter structure, see second article}}; bool move_next () {return coro? (coro.resume (),! coro.done (): false;} int current_value () {return coro.promise (). Current_value;} generator (generator const &) = delete; generator (generator & & rhs): coro (rhs.coro) {rhs.coro = nullptr;} ~ generator () {if (coro) coro.destroy ();} private: generator (handle h): coro (h) {} handle coro;} Generator f () {co_yield 1; co_yield 2;} int main () {auto g = f (); / / stop at initial_suspend while (g.move_next ()) / / each call stops at the next co_await std::cout

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