0

I need to use Lua in IOCP, and use Thread local Storage to store lua_State *. I should use lua_close() destroy the lua_State before the thread destroyed, but the work thread is created by IOCP.

The question is when is the right time to call lua_close?

static DWORD WINAPI work_thread_proc(void* parameter){
    lua_State * L = TlsGetValue(tls_lua_key);
    if(NULL = L){
          L=luaL_newstate();
          //DO some initialze for L...

          TlsSetValue(tls_lua_key,L);
    }

}


//..... other place call
QueueUserWorkItem(&work_thread_proc, req, WT_EXECUTELONGFUNCTION);
greatwolf
  • 20,287
  • 13
  • 71
  • 105
bywayboy
  • 3
  • 4

1 Answers1

0

you can use it like this.

__declspec(thread) lua_State *tls_LuaState = NULL;
// close lua state on exit thread.
void NTAPI TLS_CloseLuaState(PVOID module, DWORD reason, PVOID reserved)
{
    if(NULL != ){
        lua_close(tls_LuaState);
    }
}

#pragma section(".CRT$XLB",long,read)
    __declspec(allocate(".CRT$XLB"))
        PIMAGE_TLS_CALLBACK p_thread_callback_base = TLS_CloseLuaState;
bywayboy
  • 3
  • 4