In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
How to learn C++Coroutine, many novices are not very clear about this, in order to help you solve this problem, the following editor will explain for you in detail, people with this need can come to learn, I hope you can gain something.
In C++, a function whose body implementation contains any of the keywords co_await, co_yield, or co_return, then the function is a coroutine. Where:
Co_await: suspending the current coroutine
Co_return: returns a result from the current coroutine
Co_yield: returns a result and suspends the current coroutine
A coroutine needs to be recognized and compiled at compile time, and in some cases you have to specialize the coroutine_traits yourself. Here's a simple coroutine to talk about how the C + + compiler modifies the coroutine.
/ / Let's assume that the template function is a coroutinetemplate TRet func (TArgs args...) {body; / / body contains at least one of co_await, co_yield, and co_return. }
Then the function will be changed by the compiler to the following form:
/ / it will be expanded into the following form at compile time: template TRet func (TArgs args...) {using promise_t = typename coroutine_traits::promise_type; promise_t promise; auto _ return__ = promise.get_return_object (); / / this _ return__ will be specially handled by the compiler co_await promise.initial_suspend (); try {/ / co_return expr; = > promise.return_value (expr); goto final_suspend; body; / / co_return = > promise.return_void (); goto final_suspend;} / / co_yield expr; = > co_await promise.yield_value (expr); catch (...) {promise.set_exception (std::current_exception ());} final_suspend: co_await promise.final_suspend ();}
The above is the basic form of a coroutine. In fact, after reading it, you will find that the key to a coroutine is mainly the promise associated with it.
Another concept associated with coroutine promise is called awaitable. Awaitable can be called a waitable object. An awaitable object needs to implement three related functions:
1. Whether the await_ready:awaitable instance has been ready
2. Await_suspend: suspend awaitable. This function passes in an argument of type coroutine_handle. This is a variable generated by the compiler. The suspend procedure can specify when, where, and how the coroutine is resume. For example, when implementing the suspend function, put coroutine_handle into threadpool. Then the current coroutine is then run in the background thread assigned by the thread pool.
3. Await_resume: this function is called when coroutine is run again.
So there are three ways to make a type awaitable:
1. When the code related to this type cannot be modified, you need to implement:
Bool await_ready (T &)
Void await_suspend (T &, coroutine_handle)
Auto await_resume (T &); auto depends on the situation.
2. When the code related to this type can be modified, you need to add 3 member functions:
Bool await_ready ()
Void await_suspend (coroutine_handle ch)
Auto await_resume ()
3. Implement the operator co_await operator, return a waitable proxy type, and implement the above three functions.
Is it helpful for you to read the above content? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.