I've been learning about what's new in C++20 and I'm trying to understand the commonly discussed "Generator" use case of co-routines. I've tried to create a small example here but apologies if there is some mistake:
generator<int> Generate() {
int i = 0;
while(1) {
co_yield i++;
}
}
int main()
{
auto gen { Generate() };
for (int x = 0; x < 10; ++x) {
gen.next();
std::cout << gen.getValue() << std::endl;
}
return 0;
}
But I don't see how this is any different from a function with static variables like:
auto gen() {
static int i = 0;
return i++;
}
int main()
{
for (int x = 0; x < 10; ++x)
std::cout << gen() << std::endl;
return 0;
}
I think I can maybe see how asynchronous I/O is a usecase, especially with the co_await
keyword, but for this generator example I'm sure I'm misunderstanding something about how they should be used. I'd be very grateful for any explanation
If a static variable is defined outside of the functions it will be accessible only by the code that follows in the file it is declared. If the static variable is declared in a function, it will only be accessible from the function, and it will keep its value between function executions.
Internal static variables are active(visibility) in the particular function. External Static variables are active(visibility)throughout the entire program. Internal static variables are alive(lifetime) until the end of the function. External static variables are alive(lifetime) in the entire program.
Coroutines (C++20) A coroutine is a function that can suspend execution to be resumed later. Coroutines are stackless: they suspend execution by returning to the caller and the data that is required to resume execution is stored separately from the stack.
A coroutine is a concurrency design pattern that you can use on Android to simplify code that executes asynchronously. Coroutines were added to Kotlin in version 1.3 and are based on established concepts from other languages.
Perhaps the most obvious difference is that static
local variables means you effectively have one instance... total. Whereas each generator is completely independent.
// with coroutines
assert(Generator().next() == 0);
assert(Generator().next() == 0);
assert(Generator().next() == 0);
assert(Generator().next() == 0);
Every call to Generator()
is creating a new generator, each of which starts counting at 0
. So every new generator's next()
gives me zero. As expected.
But that's not the case with static local variables:
assert(gen() == 0);
assert(gen() == 1);
assert(gen() == 2);
assert(gen() == 3);
So you could imagine how if what you wanted was to create a generator that gave you an infinite stream of integers, it'd be nice if you could use that function reliably more than one time total in your entire program.
This isn't to say that static local variables aren't useful. It's just that they're not usable for this particular use-case.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With