2020-11-20 04:12:56 +08:00
|
|
|
#define SOL_ALL_SAFETIES_ON 1
|
|
|
|
#include <sol/sol.hpp>
|
|
|
|
|
|
|
|
#include <string>
|
|
|
|
#include <iostream>
|
|
|
|
|
2021-03-06 14:03:23 +08:00
|
|
|
int main() {
|
2021-03-06 23:14:48 +08:00
|
|
|
std::cout << "=== coroutine - multple threads ==="
|
|
|
|
<< std::endl;
|
2020-11-20 04:12:56 +08:00
|
|
|
|
|
|
|
sol::state lua;
|
2021-03-06 23:14:48 +08:00
|
|
|
lua.open_libraries(sol::lib::base,
|
|
|
|
sol::lib::package,
|
|
|
|
sol::lib::coroutine);
|
|
|
|
|
|
|
|
lua["print"] = [](sol::object v) {
|
|
|
|
std::cout << v.as<std::string>() << std::endl;
|
|
|
|
};
|
|
|
|
lua["cyield"] = sol::yielding(
|
|
|
|
[]() { std::cout << "YIELDING" << std::endl; });
|
2020-11-20 04:12:56 +08:00
|
|
|
|
|
|
|
// notice the new threads!
|
|
|
|
sol::thread thread1 = sol::thread::create(lua);
|
|
|
|
sol::thread thread2 = sol::thread::create(lua);
|
|
|
|
|
|
|
|
// notice we load it FROM the new "execution stack"
|
|
|
|
// we need it to have thread1's stack perspective
|
|
|
|
sol::coroutine co1 = thread1.state().load(R"(
|
|
|
|
print("AA : Step 1")
|
|
|
|
cyield()
|
|
|
|
print("AA : Step 2")
|
|
|
|
)");
|
|
|
|
// call first coroutine here
|
|
|
|
co1();
|
|
|
|
|
|
|
|
// notice we load it FROM the new "execution stack"
|
|
|
|
// we need it to have thread2's stack and perspective
|
|
|
|
sol::coroutine co2 = thread2.state().load(R"(
|
|
|
|
print("BB : Step 1")
|
|
|
|
cyield()
|
|
|
|
print("BB : Step 2")
|
|
|
|
)");
|
|
|
|
|
|
|
|
// run the other coroutine
|
|
|
|
co2();
|
|
|
|
co1();
|
|
|
|
// tada! they run on
|
|
|
|
// independent stacks
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|