回答
Boost :: Context是Boost版本1.51.0及更高版本的官方部分。有關它的信息,請參閱http://www.boost.org/doc/libs/1_51_0/libs/context/doc/html/index.html。不幸的是,文檔是略微不同於實現,有些東西已經在SVN中發生了變化,所以您需要稍微閱讀頭文件。
下面是我寫了一天的例子顯示的boost ::語境下使用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.
作者的website上的boost-coroutine存檔包含一些基本的文檔和示例,這些文檔和示例都是建立在上下文和上下文本身之上的協程。您還可以在該網站上找到一個光纖套件,至少作爲另一個用例可能很有趣。
謝謝。但是,您提供的鏈接似乎不起作用。 – 2012-08-01 02:10:11
無賴。這個對我有用。請注意,包括基本示例和文檔的上下文到目前爲止已經成爲助推器。如果你不想通過SVN結賬,你也可以在線瀏覽它[這裏](http://svn.boost.org/svn/boost/trunk/libs/context/)。然而,協程和纖維不在樹幹中(但)。 – ingoem 2012-08-01 07:53:40
順便說一句,它似乎在協同和光纖上的活躍發展發生在Oliver Kowalke的[git倉庫](http://gitorious.org/~k-oli/boost-dev/)。 – ingoem 2012-08-01 09:14:39
謝謝你的例子代碼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";
}
- 1. Boost上下文實現
- 2. EF類上下文
- 3. Android ITelephony類上下文
- 4. 在Linux下boost :: interprocess :: create_or_open_file更改文件類型
- 5. boost :: archive :: xml_iarchive失敗,帶有一個xml(deserizalisation上下文)
- 6. Boost庫類型
- 7. boost ::可選參考與boost :: variant類型
- 8. 的boost ::工廠創建的boost :: enable_shared_from_this類
- 9. 在類boost序列化類
- 10. 在ES6類中處理上下文
- 11. 頁的種類上下文的請求
- 12. 模型類中的EF上下文
- 13. Android/Java類的範圍和上下文
- 14. 在Python 2.4中處理上下文類
- 15. 約束返回類型的上下文
- 16. web.xml加載類啓動到上下文
- 17. 將上下文傳遞給異步類
- 18. JavaScript類和上下文的問題
- 19. ExecutionContext類如何與「上下文」相關?
- 20. 在異步類中檢索上下文
- 21. getSupportFragmentManager() - 未定義類型的上下文
- 22. 是否爲null類型的上下文?
- 23. 測試SQLiteOpenHelper類時上下文null null
- 24. Java執行上下文類解釋
- 25. GetPackageName包含上下文的類名稱
- 26. Fragment類中的加載器上下文
- 27. 未找到AWS Java SDK上下文類
- 28. 上下文相關幫助 - PHP類
- 29. Drupal的上下文 - 上下文
- 30. Android上下文和上下文PowerManager
聲明棧在哪裏? – Lucretiel 2013-02-01 15:45:13
@Lucretiel它是類的成員,在代碼中:'std :: array stack;'=) –
wjl
2013-02-02 05:57:57
哦。我完全沒有看到滾動條 – Lucretiel 2013-02-04 22:40:49