Skip to main content

new_thread

Function new_thread 

Source
pub fn new_thread(
    state: &mut LuaState,
    initial_body: Option<LuaValue>,
) -> Result<(), LuaError>
Expand description

Create a new coroutine thread sharing the same GlobalState as the caller.

Pushes the new thread onto the caller’s stack and returns Ok(()).


//   global_State *g = G(L);
//   GCObject *o;
//   lua_State *L1;
//   lua_lock(L); luaC_checkGC(L);
//   o = luaC_newobjdt(L, LUA_TTHREAD, sizeof(LX), offsetof(LX, l));
//   L1 = gco2th(o);
//   setthvalue2s(L, L->top.p, L1); api_incr_top(L);
//   preinit_thread(L1, g);
//   ... (copy hook settings, extra space, stack_init) ...
//   lua_unlock(L); return L1;
// }

Allocate a fresh coroutine LuaState, register it under a new ThreadId, and push the resulting LuaValue::Thread(value) onto state’s stack.

If initial_body is Some(f), f is also pushed onto the new thread’s stack so that coroutine.status reports "suspended" rather than "dead". The full cross-thread xmove from caller to coroutine arrives in slice 02b; co_create uses initial_body to stage the body without needing a real xmove.