3

Can anyone give me an example of how I can use segmented stacks with boost coroutines? Do I have to annotate every function that is called from the coroutine with a special split-stack attribute?

When I try and write a program that should use segmented stacks, it just segfaults.


Here is what I have done so far https://wandbox.org/permlink/TltQwGpy4hRoHgDY The code seems to segfault very quickly, if segmented stacks were used I would expect it to be able to handle more iterations. The program errors out after 35 iterations.

#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);
}
Curious
  • 20,870
  • 8
  • 61
  • 146
  • Are you having trouble with some aspect of using segmented stacks with boost coroutines or have you not written an attempt to do it yet? – TylerH Sep 18 '17 at 19:00
  • @TylerH when I try and cause a stack overflow it just happens, not sure how to use segmented stacks. From reading the documentation here https://llvm.org/docs/SegmentedStacks.html it seems like functions need special annotations to be considered "stacklets" , just wanted an example of how other people have accomplished this – Curious Sep 18 '17 at 19:02
  • 4
    I'm not well-versed in C++, but I'm afraid if you are looking for others' implementations, that makes this question too broad/primarily opinion-based (e.g. "how would *you* do it"). If you can rephrase your question to show a specific problem/demo code with an implementation attempt, it'd be easier to provide an answer that helps solve the problem you're encountering. – TylerH Sep 18 '17 at 19:08
  • 1
    @TylerH posted an example, does that suffice? – Curious Sep 18 '17 at 19:14
  • Thanks; perhaps also edit the question title to better describe the problem you are seeing? E.g. "why is my coroutine code segfaulting so quickly" or something, to better catch the eye of folks browsing the [tag:c++] questions. – TylerH Sep 18 '17 at 19:16
  • @TylerH ok, better? – Curious Sep 18 '17 at 19:17
  • The title still seems overly broad to me; though I admit my knowledge is limited, I think asking a more *specific* question in the title is better; broad questions like "how to do X" rarely get good, specific answers. If I saw the title, I'd think "this person wants a whole tutorial", which may *be* what you want in reality, but it's not an on-topic thing for Stack Overflow. :-P – TylerH Sep 18 '17 at 19:18

2 Answers2

3

Compiling that code with -fsplit-stack solves the problem. Annotations are not required. All functions are by default treated as split stacks. Example - https://wandbox.org/permlink/Pzzj5gMoUAyU0h7Q

Easy as that.

Curious
  • 20,870
  • 8
  • 61
  • 146
0

compile boost (boost.context and boost.coroutine) with b2 property segmented-stacks=on (enables special code inside boost.coroutine and boost.context).

your app has to be compiled with -DBOOST_USE_SEGMENTED_STACKS and -fsplit-stack (required by boost.coroutines headers).

see documentation: http://www.boost.org/doc/libs/1_65_1/libs/coroutine/doc/html/coroutine/stack/segmented_stack_allocator.html

boost.coroutine contains an example that demonstrates segmented stacks (in directory coroutine/example/asymmetric/ call b2 toolset=gcc segmented-stacks=on).

please note: while llvm supports segmented stacks, clang seams not to provide the __splitstack_<xyz> functions.

xlrg
  • 1,994
  • 1
  • 16
  • 14