Spawning a thread and call a function from lua #406
-
Hi, I want to know if it's possible to export a I tried something like this: use mlua::prelude::*;
fn spawn_thread(_: &Lua, func: LuaFunction) -> LuaResult<()> {
println!("Spawning thread...");
std::thread::spawn(move || {
println!("Thread spawned");
let _ = func.call(()).unwrap();
println!("Thread finished");
});
Ok(())
}
#[mlua::lua_module]
fn my_module(lua: &Lua) -> LuaResult<LuaTable> {
let exports = lua.create_table()?;
exports.set("spawn_thread", lua.create_function(spawn_thread)?)?;
Ok(exports)
} but I get the following error from the compiler:
I'm using the following features: Any example about how can I achive multi thread like this will be appreciated. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Lua (and mlua) is strictly |
Beta Was this translation helpful? Give feedback.
Lua (and mlua) is strictly
!Sync
, you cannot do this.To achieve true parallelism you can spawn another Lua interpreter to execute function (passed as source code) then serialize results and pass back to main VM.