sol2/examples/source/coroutine_multiple_stacks.cpp
ThePhD 62804667ee
Fix various issues
- readonly(...) should no longer issue errors in bogus manners
- binding for member function pointers and normal pointers should behave better now
- coroutines should now properly transfer the state/identity of their error handlers
- Fix empty base class optimization type
- Improve bind traits metaprogramming
- Add new define to catch-all for exception propagation
- Improve natvis
- Move test defines to build system, as we should
- Add a new example for multiple thread/states in Coroutines
- Add proper direct to mangling in Clang for Windows
- Exceptions are still busted on some platforms (Clang being quite funky)
2020-11-19 15:12:56 -05:00

50 lines
1.2 KiB
C++

#define SOL_ALL_SAFETIES_ON 1
#include <sol/sol.hpp>
#include <string>
#include <iostream>
int main () {
std::cout << "=== coroutine - multple threads ===" << std::endl;
sol::state lua;
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;
});
// 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;
}