MCPcopy Create free account
hub / github.com/ThePhD/sol2 / main

Function main

examples/source/docs/coroutine_thread.cpp:6–39  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

4#include <iostream>
5
6int main() {
7 const auto& co_lua_script = R"(
8function loop()
9 while counter ~= 30
10 do
11 coroutine.yield(counter);
12 counter = counter + 1;
13 end
14 return counter
15end
16)";
17
18 sol::state lua;
19 lua.open_libraries(sol::lib::base, sol::lib::coroutine);
20 /*
21 lua.script_file("co.lua");
22 we load string directly rather than use a file
23 */
24 lua.script(co_lua_script);
25 sol::thread runner = sol::thread::create(lua.lua_state());
26 sol::state_view runnerstate = runner.state();
27 sol::coroutine loop_coroutine = runnerstate["loop"];
28 lua["counter"] = 20;
29
30 for (int counter = 0; counter < 10 && loop_coroutine;
31 ++counter) {
32 // Call the coroutine, does the computation and then
33 // suspends
34 int value = loop_coroutine();
35 std::cout << "value is " << value << std::endl;
36 }
37
38 return 0;
39}

Callers

nothing calls this directly

Calls 4

open_librariesMethod · 0.80
scriptMethod · 0.80
lua_stateMethod · 0.45
stateMethod · 0.45

Tested by

no test coverage detected