How to use boost context correctly

I want to implement a quest system for my game engine using fibers. After searching the web for a good fiber implementation, I found that Boost.Context is a good starting point.

Update 1 : I want to implement my own scheduling algorithm, so Boost.Fiber , Boost.Coroutine , Boost.Coroutine2 not suitable for my implementation.

After compiling boost for x64 architecture and trying to run a basic example from boost documentation , I got the following exception:

boost :: context :: detail :: forced_unwind in memory location

This is the code I was trying to run (Visual Studio 2015 version, Windows 7):

#include <iostream>
#include <boost\context\continuation.hpp>
namespace ctx=boost::context;
int main()
{
    ctx::continuation source=ctx::callcc
    (
        [](ctx::continuation && sink)
        {
            int a=0;
            int b=1;
            for(;;)
            {
                sink=sink.resume(a);
                auto next=a+b;
                a=b;
                b=next;
            }
            return std::move(sink);
        }
    );
    for (int j=0;j<10;++j) {
        std::cout << source.get_data<int>() << " ";
        source=source.resume();
    }
    return 0;
}

      

The code worked correctly (correct output: 0 1 1 2 3 5 8 13 21 34 55), but when it finished the run I got an exception.

Update 2: Exception only happens for release build

I want to ask two questions regarding boost context:

1) What caused the stack erase exception and how to avoid it?

2) I found the acceleration documentation to be a bit shallow and couldn't find another tutorial on how to use boost context. Can you point me to some good sources / tutorials on context acceleration?

+3


source to share


3 answers


First, the forced disconnect exception is documented (and this is what gives contextual RAII support).

Secondly, if you want to use Fibers built from Boost Context, use the existing libraries:



Update:

Testing with Boost 1.64.0 in Microsoft (R) C / C ++ Compiler Optimization Version 19.00.24215.1 for x64 I was unable to reproduce the behavior:

enter image description here

0


source


After 0 is returned, the original object is not at the position to be destroyed.

Something like:



namespace ctx = boost::context;
int a;
bool stop = false;
{
    ctx::continuation source = ctx::callcc(
        [&stop, &a](ctx::continuation && sink) {
        a = 0;
        int b = 1;
        for (;;) {
            sink = sink.resume();
            if (stop)
                break;
            int next = a + b;
            a = b;
            b = next;
        }
        return std::move(sink);
    });
    for (int j = 0; j < 10; ++j) {
        std::cout << a << " ";
        source = source.resume();
    }
    stop = true;
    source.resume();
}

      

0


source


If you are on Windows and the error only occurs in release mode, you may need to verify that the compiler flags are set correctly.

Hence :

Windows using fcontext_t: Disable global program optimization (/ GL) and change / EHsc (the compiler assumes that functions declared extern "C" never throw a C ++ exception) in / EHs (tells the compiler that functions are declared extern " C might throw an exception).

0


source







All Articles