c++ - boost 继续恢复断言失败

标签 c++ boost

我尝试测试延续,我的代码在下面

ctx::continuation fc1; 
ctx::continuation fc2;

ctx::continuation foo1(ctx::continuation&& c) {
    LOG_DBUG("enter");
    c = c.resume();
    LOG_DBUG("done 1");
    fc2 = fc2.resume();
    LOG_DBUG("done 2");
    fc2 = fc2.resume();
    return std::move(c);
}

ctx::continuation foo2(ctx::continuation&& c) {
    LOG_DBUG("enter");
    c = c.resume();
    LOG_DBUG("done 1");
    fc1 = fc1.resume();
    LOG_DBUG("done 2");
    fc1 = fc1.resume();
    return std::move(c);
}


int main() {
    fc1 = ctx::callcc(foo1);
    fc2 = ctx::callcc(foo2);

    LOG_INFO("after callcc");

    for (;;) {
        while(fc1 = fc1.resume()) LOG_DBUG("main c1");
        while(fc2 = fc2.resume()) LOG_DBUG("main c2");
        break;
    }
    std::cout << "main: done" << std::endl;
    return 0;
}

得到标准输出

DBUG [coepoll.cpp:36] [foo1]输入

DBUG [coepoll.cpp:46] [foo2]输入

INFO [coepoll.cpp:61] [main]after callcc

DBUG [coepoll.cpp:38] [foo1]完成 1

DBUG [coepoll.cpp:48] [foo2]完成 1

./myboost/include/boost/context/continuation_fcontext.hpp:263: boost::context::continuation boost::context::continuation::resume() &&: 断言 `nullptr != fctx_' 失败。

似乎在函数 foo2 调用 fc1 = fc1.resume() 导致断言失败。 我的代码有什么错误吗?

最佳答案

当调用resume时,continuation对象失效。

Reference说:

continuation is a one-shot continuation - it can be used only once, after calling continuation::resume() or continuation::resume_with() it is invalidated.

以下伪代码显示了resume 函数的工作原理:

 foo(continutation&& mainContext) {
            mainContext = mainContext.resume(); 
     //                 | 
     //                 |   [2] resume is called;
     //                 |   [2] resume invalidates mainContext; 
     //                 |   [2] no return value is assigned to mainContext!
     //                 |   [2] we go back to main
     // [4]             |
     // make assignment |
     // now mainContext | 
     // is valid        |
 }

int main(){
    fooContext = callcc( foo );          // [1] we enter into foo function;
    fooContext = fooContext.resume();    // [3] we go back to foo 
}

下面是你的代码的简化版本,让我们分析一下:

ctx::continuation foo1(ctx::continuation&& main) {
    main = main.resume(); // [2]
    fc2 = fc2.resume(); // [6]
    fc2 = fc2.resume();
    return std::move(c);
}

ctx::continuation foo2(ctx::continuation&& main) {
    main = main.resume(); // [4]
    fc1 = fc1.resume(); // [7]
    fc1 = fc1.resume();
    return std::move(c);
}


int main() {
    fc1 = ctx::callcc(foo1); // [1]
    fc2 = ctx::callcc(foo2); // [3]

    for (;;) {
        while(fc1 = fc1.resume()) LOG_DBUG("main c1"); // [5]
        while(fc2 = fc2.resume()) LOG_DBUG("main c2");
        break;
    }
  1. foo1 开始。
  2. 执行返回到main
  3. foo2 启动。
  4. 执行返回到main
  5. resumefc1 上调用。 fc1 无效。控制执行返回到 [6]。
  6. foo2 这行中恢复执行,我们跳转到 [7]。
  7. resumeinvalidated fc1 上调用,程序中止。

关于c++ - boost 继续恢复断言失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58414501/

相关文章:

c++ - gdb - 获取真正的崩溃行

string - async_ read_until 没有按预期工作

c++ - 如何在boost regex_search中获取原始字符串前缀

c++ - 具有不同签名的函数对象参数的函数

c++ - C++中使用跳转表实现switch语句

c++ - 从 UMDF 驱动程序 (C++) 调用 CreateFile 时出现“访问被拒绝”错误

c++ - glfw openGL c++ 窗口背景和标题

c++ - 混合类和函数模板

c++ - Boost 是否支持 Windows EnterCriticalSection API?

c++ - 为什么在 C++ 中使用 Boost 文件系统会出现链接器错误?