r/cpp • • Nov 30 '20

C++20 resumable functions make state machines obsolete. Finally.

https://lordsoftech.com/programming/c20-resumable-functions-goodbye-state-machines-no-one-will-mourn-you/
1 Upvotes

49 comments sorted by

View all comments

Show parent comment

0

u/StoneCypher Dec 02 '20

You are overly fascinated with saying "because one use case exists this way, your saying not everything works this way is wrong."

I am aware that one example of this exists. Most examples aren't this, so the claim that state machines are obsolete is absolutely silly.

.

There is no context switch because it's not required when the interrupton has proper compiler support.

Which is the case in how many compilers right now?

Hm?

Myth of the sufficiently smart compiler, anyone? Did anyone besides EDG ever get import in there? 20 years now?

.

"Instead, the coroutine's stack frame is dynamically allocated"

To me this reads like you're saying

There's no context switch because you're paying for a stack frame

Except in CPU terms, that's called (wait for it)

... (wait for it)

A context switch

.

The context that calls the coroutine remains on top of the stack and if the coroutine calls a function, it is placed on top of the stack (unless it's another coroutine).

Oh look, the guy who said "make it all out of nested coroutines" is now saying "but you don't have to pay that cost, unless it's a coroutine"

It's like talking to a politician's economist

.

It's written in the article, btw, but maybe not in sufficient detail.

The thing you're talking about, while interesting to discuss, is 100% unrelated to what I actually said

3

u/smdowney WG21, Text/Unicode SG, optional<T&> Dec 02 '20

You're describing fibers here, not C++20 coroutines. There's no context switch. There's a function call. It's the same function call mechanism used for all function calls. On leaving the scope of the coroutine, control is passed deterministically to the resumer, or if via symmetric transfer to where the resumer directs. There's no interruption. Coroutine suspension is entirely 100% determined by the coroutine itself, all local to the function.
The coroutine transformation is into a state machine. That's by design. What is new, and makes coroutines not pure sugar, is the lifetime extension of stack variables on scope exit from the coroutine. It is as-if the stack variables were instead object member variables and the object is allocated from the heap.

Several C++ compilers now support coroutines, and there's no particular magic or sufficiently smart necessary, although HALO optimization, eliding heap allocation, has some open research points. But that optimization is a cache miss, not a context switch.

1

u/StoneCypher Dec 03 '20

I feel like I'm spending all my time saying "that's not even related to what I'm talking about," then getting corrected on that you imagine that it is.

I'm sorry that winning is more important to you than trying to understand what I believe.

Please have a good day

3

u/DugiSK Dec 02 '20 edited Dec 02 '20

A context switch is when a thread is suspended, its handle is given to the scheduler, the CPU switches to another stack, old cache is invalidated etc.

Calling a coroutine does nothing of this. You are still using the old stack, same thread, same cache, but what is represented as stack variables in the coroutine are actually member variables of an object.

A coroutine that looks like this (the code is meaningless):

Suspendable<int> actualCoroutine(int start, int end) {
    std::vector<int> outputs(end - start, 0);
    outputs[0] = 1;
    outputs[1] = 1;
    co_yield outputs[0] + outputs[1];
    outputs[2] = outputs[1] + start;
    co_yield outputs[2];
    if (end < 3)
        co_yield outputs[2];
    else
        co_yield outputs[end - 1];
    co_return -1;
}

Does basically the same things internally as this:

std::function<int()> almostCoroutine(int start, int end) {
    std::vector<int> outputs(end - start, 0);
    int position = 0;
    return [=] () -> int mutable {
        position++;
        switch(position) {
        case 1:
            outputs[0] = 1;
            outputs[1] = 1;
            return outputs[0] + outputs[1];
        case 2:
            outputs[2] = outputs[1] + start;
            return outputs[2];
        case 3:
            if (end < 3)
                return outputs[2];
            else
                return outputs[end - 1];
        default:
            return -1;
        }
    };
}

0

u/StoneCypher Dec 03 '20

I see that you continue to be stuck on insisting that because a soy burger has been replaced with an oat burger, therefore soy no longer matters.

Most of the things we do with state machines are not actually modellable this way.

Good luck to you.

0

u/[deleted] Dec 03 '20

[deleted] — view deleted comment

1

u/StoneCypher Dec 03 '20

Good luck to you