提升上下文类

时间:2012-07-30 06:21:59

标签: c++ boost boost-context

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

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

3 个答案:

答案 0 :(得分:15)

Boost :: Context是版本1.51.0及更高版本中Boost的官方部分。有关它的信息,请参阅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.

答案 1 :(得分:2)

作者boost-coroutine上的website存档包含一些基于上下文的协程的基本文档和示例,以及上下文本身。您还可以在该网站上找到一个光纤包,至少作为另一个用例可能很有趣。

答案 2 :(得分:0)

谢谢你wjl你的示例代码。它帮助我了解了boost上下文如何工作以及如何通过boost上下文实现boost协程。但是你的代码没有按原样运行,所以我将其修改为在Windows上编译。 (虽然没有检查它是否适用于Linux)

#include <iostream>
#include <array>
#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";
}
相关问题