[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Multithreading lua scripts from C
- From: Tim Hill <drtimhill@...>
- Date: Tue, 18 Feb 2014 23:50:07 -0800
On Feb 18, 2014, at 11:19 PM, Milind Gupta <milind.gupta@gmail.com> wrote:
> Hi,
> I attempted at running multiple lua scripts from C. Pasted below is the code I have. The 2 lua scripts are in the strings prog1 and prog2 which are basically 2 unequal loops. I create threads and set hooks for the thread for a mask count of 4. I resume each alternately but I do not get the expected result at all. I never see an output from prog2 and prog1 i somehow becomes a decimal number. The output is as follows:
>
> And the program is as follows:
>
>
> int main(int argc, char* argv[ ])
> {
> luaVM1 = lua_open();
> luaVM2 = lua_open();
>
> luaL_openlibs(luaVM1);
> luaL_openlibs(luaVM2);
>
> thread1 = lua_newthread(luaVM1);
> thread2 = lua_newthread(luaVM2);
>
lua_open() (which is really just lua_newstate() under it’s old name) creates both an independent Lua state *and* it’s first thread. If you want coroutines *within* that state you should use lua_newthread() to create the additional threads. So call lua_newstate() once and lua_newthread() once.
lua_open()/lua_newstate() create brand-new, independent Lua states that do not share anything (not even globals or the GC state). lua_newthread() adds another thread to an existing Lua state in the same way lua_newtable() adds a new table.
—Tim