42{
43 std::cout << "=== coroutine ===" << std::endl;
44
45 sol::state lua;
46 std::vector<sol::coroutine> tasks;
47
48 lua.open_libraries(sol::lib::base, sol::lib::coroutine, sol::lib::jit, sol::lib::math);
49
50 sol::thread runner_thread = sol::thread::create(lua);
51
52 lua.set_function("start_task",
53 [&runner_thread, &tasks](sol::function f, sol::variadic_args va) {
54
55 sol::state_view runner_thread_state = runner_thread.state();
56
57 std::size_t task_index = tasks.size();
58 tasks.emplace_back(runner_thread_state, f);
59 sol::coroutine& f_on_runner_thread = tasks[task_index];
60
61
62
63
64
65
66 int wait = f_on_runner_thread(va);
67 std::cout << "First return: " << wait << std::endl;
68
69
70 f_on_runner_thread();
71 std::cout << "Second run complete: " << wait << std::endl;
72 });
73
74 lua.script(
75 R"(
76 function main(x, y, z)
77 -- do something
78 coroutine.yield(20)
79 -- do something else
80 -- do ...
81 print(x, y, z)
82 end
83 function main2(x, y)
84 coroutine.yield(10)