Spawning a thread and call a function from lua #406
Answered
by
khvzak
ghosty2004
asked this question in
Q&A
|
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. |
Answered by
khvzak
May 12, 2024
Replies: 1 comment 1 reply
|
Lua (and mlua) is strictly |
1 reply
Answer selected by
ghosty2004
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.