lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


One solution I figured could be to have the callback results be stored in a queue (C side), and then fire a TCP packet on a luasocket, this would make the select-call return and indicate that there are callback results to collect from the queue. Would work but feels like an ugly workaround.

 

Is there a common practice for solving this type of issue?


An app that I've been working on for the past few months uses a single Lua state as a host for multiple worker threads, each with their own callbacks and a (rather simple) event structure. The app is quite thread-safe - Any function that accesses a given Lua state is wrapped in a lock statement, with each state having its own locking object. From there it's all pretty easy to work with. Example below [1].

While my app is written C# using LuaInterface, you can duplicate this effect in C/C++ quite easily with a mutex or any number of other approaches. Applying Google-Fu to "C++ lock" yields quite a few promising methods (one of which even duplicates the C# lock syntax [2]). I'd take a look into those and find one that works best for you.

Aside: Locks also do wonders for logging in a multithreaded app.

[1]: http://pastebin.com/uk4HetRS
[2]: http://www.curly-brace.com/lock.html