Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the difference between a coroutine and a function with static variables?

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

like image 565
Robert Avatar asked Apr 30 '21 21:04

Robert


People also ask

Can static variables be used outside a function?

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.

What is the difference between a static inside a function scope and a static outside that scope?

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.

What are C++ coroutines good for?

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.

What is coroutine in programming?

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.


1 Answers

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.

like image 199
Barry Avatar answered Nov 02 '22 03:11

Barry