lua-users home
lua-l archive

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


Hi,
after longer thinking about our strategy and how to implement Lua in our windows software, I am now ready with the first demo software and in fact really still extremely impressed of Lua, so very happy all around. (also Lua programming with VS Code and sumneko Lua extension really very nice, also easy possibilities to create help/intellisense info for my own lua functions).

For me it is important, that several "Lua threads" can be started by some user actions, and then these "Lua threads" should somehow run "in parallel". I want only ONE Lua file, and each thread should run its own function (also ok if several threads run the same Lua function).

So the Lua threads must not block each other, and they need this resume-yieldk procedure which is nicely described in Roberto's "Programming in Lua" in chapter 33, end of paragraph "Multiple Threads" at the very end. The description is very short, but this runs very perfectly and seen from my view is a very nice and efficient implementation, thank you to Lua team for this.

To allow one lua thread to sleep for some time, without blocking the other Lua threads, thus I implemented a function called sleep( tm_sec) - this function then will in my Thread Loop be called by lua_resume every 10msec (this is the "standard Windows" minimum settimer interval time) to check whether the time has expired, otherwise it again will call lua_yieldk.

When my Lua thread is finished, I optinoally want to give some "Run summary info" to the lua programmer... . For this I would like to get some interesting "Lua run statistics":
- total run time of Lua thread (this is straight forward of course)
- maximum run time of any lua_resume - lua_yieldk execution cycle (this is also no problem to do in my C software)
- maximum number of Lua commands in any such lua_resume-lua_yieldk execution cycle.

This third parameter is somehow tricky ... I did not find a lua c command to get this. I only find the command lua_gethookcount, but this is only the number which I specify for my debug hook. I use such a debug hook every 100th Lua command, and if more than 1000 commands in such a lua_resume-lua_yieldk execution cycle, I kill the thread with some error message "possible infinite lua loop - please check". This hook is setup only for every 100th command - I do not want to put this number smaller (or to 1), as it certainly then will slow down Lua considerably.

So currently for my "end of thread statistics" I can only give info about the "maximum number of '100 lua command blocks'" during any lua_resume-lua_yieldk execution cycle (I just count my 100 command debug-hook invocations). But it really would be MUCH nicer to get this Lua command count value exactly. 

Is it possible to readout the current value of this Lua command counter in an easy way in C or Lua?