tags:

views:

174

answers:

3
+1  Q: 

Clone a lua state

Recently, I have encountered many difficulties when I was developing using C++ and Lua. My situation is: for some reason, there can be thousands of Lua-states in my C++ program. But these states should be same just after initialization. Of course, I can do luaL_loadlibs() and lua_loadfile() for each state, but that is pretty heavy(in fact, it takes a rather long time for me even just initial one state). So, I am wondering the following schema: What about keeping a separate Lua-state(the only state that has to be initialized) which is then cloned for other Lua-states, is that possible?

+1  A: 

Unfortunately, no.

You could try Pluto to serialize the whole state. It does work pretty well, but in most cases it costs roughly the same time as normal initialization.

Javier
But Pluto is just a third-party tool, you can just use it in lua script.What i want is to clone lua-state in C++ environment.
hbxfnzwpf
Why is it a problem if the functions are accessible from the Lua side? Presumably you could look at the C functions that are being registered and just call what they call to serialize the state out. If you don't want those functions accessible from Lua then don't register them for the user.Overall though, it doesn't sound like this would be the best solution.
James Snyder
A: 

When I started with Lua, like you I once wrote a program with thousands of states, had the same problem and thoughts, until I realized I was doing it totally wrong :)

Lua has coroutines and threads, you need to use these features to do what you need. They can be a bit tricky at first but you should be able to understand them in a few days, it'll be well worth your time.

Robert Gould
Coroutine is indeed a good feature of Lua, but what I am seeking for is a way to clone lua state in my C++ program.As there are thousands of lua scripts(each may be written by a different person) for me to execute in my C++ program, it is nice to load them in multi-threads. Because every lua state created should load the same lib, and load another common script before loading its corresponding script, just do lua_loadlib() and lua_load()for every lua state which returned by lua_open() is a huge burden for me, so I am wandering clone lua state directly.
hbxfnzwpf
A: 

I think it will be hard to do exactly what you're requesting here given that just copying the state would have internal references as well as potentially pointers to external data. One would need to reconstruct those internal references in order to not just have multiple states pointing to the clone source.

You could serialize out the state after one starts up and then load that into subsequent states. If initialization is really expensive, this might be worth it.

I think the closest thing to doing what you want that would be relatively easy would be to put the states in different processes by initializing one state and then forking, however your operating system supports it: http://en.wikipedia.org/wiki/Fork_(operating_system)

If you want something available from within Lua, you could try something like this: http://stackoverflow.com/questions/1242572/how-do-you-construct-a-read-write-pipe-with-lua

James Snyder