3

谁能给我一个示例,说明如何将分段堆栈与 boost 协程一起使用?split-stack我是否必须用特殊属性 注释从协程调用的每个函数?

当我尝试编写一个应该使用分段堆栈的程序时,它只是段错误。


这是我到目前为止所做 的事情https://wandbox.org/permlink/TltQwGpy4hRoHgDY 代码似乎很快就会出现段错误,如果使用分段堆栈,我希望它能够处理更多迭代。程序在 35 次迭代后出错。

#include <boost/coroutine2/all.hpp>

#include <iostream>
#include <array>

using std::cout;
using std::endl;

class Int {
    int a{2};
};

void foo(int num) {
    cout << "In iteration " << num << endl;
    std::array<Int, 1000> arr;
    static_cast<void>(arr);
    foo(num + 1);
}

int main() {
    using Coroutine_t = boost::coroutines2::coroutine<int>::push_type;
    auto coro = Coroutine_t{[&](auto& yield) {
        foo(yield.get());
    }};

    coro(0);
}
4

2 回答 2

3

编译该代码-fsplit-stack解决了这个问题。不需要注释。默认情况下,所有函数都被视为拆分堆栈。示例 - https://wandbox.org/permlink/Pzzj5gMoUAyU0h7Q

就这么简单。

于 2017-09-18T19:24:03.333 回答
0

使用 b2 属性segmented-stacks=on编译 boost(boost.context 和 boost.coroutine)(在 boost.coroutine 和 boost.context 中启用特殊代码)。

-DBOOST_USE_SEGMENTED_STACKS您的应用程序必须使用and 编译-fsplit-stack(boost.coroutines 标头需要)。

请参阅文档: http: //www.boost.org/doc/libs/1_65_1/libs/coroutine/doc/html/coroutine/stack/segmented_stack_allocator.html

boost.coroutine 包含一个演示分段堆栈的示例(在目录coroutine/example/asymmetric/ callb2 toolset=gcc segmented-stacks=on中)。

请注意:虽然 llvm 支持分段堆栈,但 clang 接缝不提供这些__splitstack_<xyz>功能。

于 2017-09-19T08:37:44.377 回答