| 58 | } |
| 59 | |
| 60 | int main() { |
| 61 | |
| 62 | // main lua state |
| 63 | sol::state lua; |
| 64 | lua.open_libraries(sol::lib::base); |
| 65 | |
| 66 | // set up functions, etc. etc. |
| 67 | lua.script("function f () return 4.5 end"); |
| 68 | lua.script("function g () return { 1.1, 2.2, 3.3 } end"); |
| 69 | |
| 70 | // kick off worker |
| 71 | worker_data data; |
| 72 | std::thread worker(worker_thread, std::ref(data)); |
| 73 | |
| 74 | // main Lua state |
| 75 | bool done_working = false; |
| 76 | for (std::uint64_t loops = 0; !done_working; ++loops) { |
| 77 | // finished working? send nothing |
| 78 | // even loop? use f |
| 79 | // otherwise, use g |
| 80 | if (loops >= 3) { |
| 81 | data.payload.clear(); |
| 82 | done_working = true; |
| 83 | } |
| 84 | else if ((loops % 2) == 0) { |
| 85 | sol::function target = lua["f"]; |
| 86 | data.payload = target.dump(); |
| 87 | } |
| 88 | else { |
| 89 | sol::function target = lua["g"]; |
| 90 | data.payload = target.dump(); |
| 91 | } |
| 92 | |
| 93 | // send data to the worker thread |
| 94 | { |
| 95 | std::lock_guard<std::mutex> lk( |
| 96 | data.until_ready_mutex); |
| 97 | data.is_ready = true; |
| 98 | std::cout |
| 99 | << "function serialized: sending to worker " |
| 100 | "thread to execute on Lua state...\n"; |
| 101 | } |
| 102 | data.until_ready_condition.notify_one(); |
| 103 | |
| 104 | if (done_working) { |
| 105 | break; |
| 106 | } |
| 107 | // wait for the worker |
| 108 | { |
| 109 | std::unique_lock<std::mutex> |
| 110 | lock_waiting_for_worker( |
| 111 | data.until_ready_mutex); |
| 112 | data.until_ready_condition.wait( |
| 113 | lock_waiting_for_worker, |
| 114 | [&data] { return data.is_processed; }); |
| 115 | data.is_processed = false; |
| 116 | } |
| 117 | auto data_processor = [](auto& returned_data) { |
nothing calls this directly
no test coverage detected