c++ - boost 上下文类

标签 c++ boost boost-context

我发现 boost 有一个名为 context 的类,用于上下文切换,对吧?

我尝试用谷歌搜索它,但没有找到任何文档或示例。我只是想知道是否有人可以提供一些信息。

最佳答案

Boost::Context 是 Boost 1.51.0 及更高版本中的官方部分。参见 http://www.boost.org/doc/libs/1_51_0/libs/context/doc/html/index.html有关它的信息。不幸的是,文档与实现略有不同,并且 SVN 中的某些内容已更改,因此您需要稍微阅读一下头文件。

这是我前几天写的一个例子,展示了 Boost::Context 使用 Boost 1.51.0+最新的 SVN 制作简单的协程:

#include <array>
#include <functional>

#include <boost/context/all.hpp>

class Coroutine {
    public:
    Coroutine() :
        my_context(boost::context::make_fcontext(
            stack.data() + stack.size(),
            stack.size(),
            Coroutine::dispatch
        ))
    {}
    virtual ~Coroutine() {}

    void operator()() {
        boost::context::jump_fcontext(&yield_context, my_context, reinterpret_cast<intptr_t>(this));
    }

    protected:
    void yield() {
        boost::context::jump_fcontext(my_context, &yield_context, 0);
    }

    virtual void call() = 0;

    private:
    static void dispatch(intptr_t coroutine_ptr) {
        Coroutine *coroutine = reinterpret_cast<Coroutine *>(coroutine_ptr);
        coroutine->call();
        while (true) coroutine->yield();
    }

    private:
    boost::context::fcontext_t *my_context;
    boost::context::fcontext_t yield_context;
    std::array<intptr_t, 64*1024> stack;
};

struct A : public Coroutine {
    void call() {
        std::cerr << "A went to the store one day.\n";
        yield();
        std::cerr << "A was looking for groceries.\n";
        yield();
        std::cerr << "A finally found what she was looking for.\n";
    }
};

struct B : public Coroutine {
    void call() {
        std::cerr << "B went to the store one day.\n";
        yield();
        std::cerr << "B was looking for replacement tires.\n";
        yield();
        std::cerr << "B didn't find anything at all.\n";
        yield();
        std::cerr << "B went to another store.\n";
        yield();
        std::cerr << "B got the tires installed there.\n";
    }
};

struct C : public Coroutine {
    void call() {
        std::cerr << "C went to the store one day.\n";
        yield();
        std::cerr << "C was looking for USB drives.\n";
        yield();
        std::cerr << "C found several with competitive pricing.\n";
        yield();
        std::cerr << "C couldn't decide which to buy, so gave up.\n";
    }
};


int main() {
    std::cerr << "So, this is what happened.\n";
    A a;
    B b;
    C c;
    for (size_t i=0; i<10; ++i) {
        a();
        b();
        c();
    }
    std::cerr << "Then it all was done.\n";
}

然后编译运行是这样的:

$ g++ -std=c++11 -o coroutines coroutines.c++ -lboost_context
$ ./coroutines
So, this is what happened.
A went to the store one day.
B went to the store one day.
C went to the store one day.
A was looking for groceries.
B was looking for replacement tires.
C was looking for USB drives.
A finally found what she was looking for.
B didn't find anything at all.
C found several with competitive pricing.
B went to another store.
C couldn't decide which to buy, so gave up.
B got the tires installed there.
Then it all was done.

关于c++ - boost 上下文类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11716291/

相关文章:

c++ - Boost ASIO/Coroutines : Attempting to write an echo server using boost asio and coroutines, 但行为不一致

c++ - 使 thread_local 变量完全易变

c++ - 是否可以重新创建使用给定 .dll/.so/.dylib 所需的 C++ 头文件?

c++ - 为什么将这两个代码的输出更改为 '&' 字符?

c++ - 虚函数和 boost 绑定(bind)奇怪的行为

c++ - boost 中的协程局部变量

C++ 将输入分离到各种变量中

c++ - glfwSetScrollCallback() 编译失败

c++ - 错误 : no matching function for call to ‘get(long unsigned int*&, long unsigned int&)’ with union_set

c++ - 在 macosx 上编译 boost_asio/example/ssl/server.cpp 在 linux 上工作