c++boostfibers

What is the difference between Boost.Context fiber and continuation?


Boost.Context provides two mechanisms for switching contexts:

  1. fiber - https://www.boost.org/doc/libs/1_74_0/libs/context/doc/html/context/ff.html
  2. continuation - https://www.boost.org/doc/libs/1_76_0/libs/context/doc/html/context/cc.html

These documentation pages have almost the same wordings for both mechanisms and examples are basically the same code with seemingly non-significant API changes.

Are there any conceptual differences between these two mechanisms? And if yes, when to use what?


Solution

  • I think fiber supersedes continuation, as the proposal says:

    This paper addresses concerns, questions and suggestions from the past meetings. The proposed API supersedes the former proposals N3985, P0099R1 and P0534R3.

    The P0534R3 is the very proposal for continuation.

    Because of name clashes with coroutine from coroutine TS, execution context from executor proposals and continuation used in the context of future::then(), fiber, as suggested by some committee members, has been chosen - filament, tasklet,lightfiber or coop_task are possible alternative names.

    So, I think fiber is all the same with continuation except for the name.

    By the way, there's a small difference between the examples for them in the documentation:

    namespace ctx=boost::context;
    int i=1;
    ctx::fiber f1{[&i](ctx::fiber&& f2){
        std::printf("inside f1,i==%d\n",i);
        i+=1;
        return std::move(f2).resume();
    }};
    f1=std::move(f1).resume();
    std::printf("i==%d\n",i);
    
    output:
        inside c1,i==1
        i==2
    
    namespace ctx=boost::context;
    int i=1;
    ctx::continuation c1=callcc([&i](ctx::continuation && c2){
                std::printf("inside c1,i==%d\n",i);
                i+=1;
                return c2.resume();
            });
    std::printf("i==%d\n",i);
    
    output:
        inside c1,i==1
        i==2
    

    It seems that callcc automatically start the continuation, while fiber needs to be start manually later.

    Here is the current status of the proposal: https://github.com/cplusplus/papers/issues/117 .