So, I came across an interesting problem. I'm implementing some threaded game logic, and one of the things I decided to do was to use Multiple Lua VMs, with each worker thread containing its own VM.

I don't intend for the scripts themselves to actually handle any data across threads. For the most part, the VMs will very rarely use the heap - as most things will be handled on the stack. The VMs are basically workhorses that drives the logic of the game, while the C++ end handles all of the data.

One of the things I noticed very quickly, is that as I get more scripts, I'm basically increasing my memory usage by a multiple of however many worker threads I am creating. This is because each State needs to load a copy of a script.

Is there a way, and how can I handle this without flooding my memory with 4+ copies of the same script? As in, one state loads in all of the scripts at the start, and puts them in memory. Then all other VMs are able to access those scripts?

  • \$\begingroup\$ I'm not a lua expert but I belive it's not possible not to load every script into every VM's, because a VM needs all information about a script to execute it. Would it be an option to create specific VM's for specific tasks? \$\endgroup\$
    – user743414
    Aug 23, 2018 at 19:14
  • \$\begingroup\$ @user743414 I figured out a way to do it using Luas built in threads, coroutines, and os threads. Its not safe but it works well rnough if you sandbox states and have minimal protected critical sections. \$\endgroup\$ Sep 18, 2018 at 14:56


You must log in to answer this question.

Browse other questions tagged .