On Sun, Aug 28, 2016 at 11:57 AM, Marc Balmer <email@example.com> wrote:
> What's the best approach to this? I think I will have to add a locking mechanism and aquire the lock before calling any of the Lua C API functions. Are there better ways?
As already explained, Lua can be recompiled so that it will use a lock when needed. An obvious drawback is that performance may suffer, but unless you measure that and find it significant, you cannot say for sure. Another drawback is that you will have to rely on a custom library, but this may be a non-issue for your project.
In a particular project where I use Lua, we can have a large number of threads interacting with a large number of Lua environments. Our Lua is custom built, so we could have used the built-in lock option. But since we have just one point of interaction between the threads and the environments, it was easier to add synchronisation there.
I think I should also warn you against a particular danger when you "aquire the lock before calling any of the Lua C API functions". If there are any functions defined by your host, which are callable from within Lua environments, these functions should better not acquire the lock, or you risk running into a deadlock or trashing your lock if it is not recursive. In the approach we adopted in our project we have no such problem because there is just one well defined point interaction.