lua-users home
lua-l archive

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


Hi,

On 26.01.20 21:28, Levente Kovacs wrote:
> Is there any way in my C application to interrupt the script, do something,
> and continue with the script?

you might want to have a look at my "mtint" module, see
https://github.com/osch/lua-mtint#mtint. This might not fit 100% your use case,
since it is meant to be a solution used from pure Lua and not from C
applications. But perhaps it might be useful somehow.

Your email inspired me to extend the "mtint" module to also allow installation
of an interrupt handler function that makes it possible to continue the
interrupted thread.

With "mtint" it is possible to interrupt another thread or coroutine from a
concurrently running thread. This can be useful for applications with
interactive user interface when a user wants to abort a long running background
task or a user supplied script that is stuck in an infinite loop.

See Example 3 (https://github.com/osch/lua-mtint#example-3): here a
"computation" background thread is started. The background thread registers an
interrupt handler function and gives its interruptible id back to the main
thread. Then the computation is started and runs in an infinite loop. The main
thread then interrupts the computation thread several times by using the
interuptible id and communicates with the interrupt handler function to give
commands to continue or to quit the computation.

For this example other Lua modules are needed for 1.) starting a concurrently
running thread and for 2.) inter-thread communication. For the example I used
"lua-llthreads2" (see https://luarocks.org/modules/moteus/lua-llthreads2) and
"mtmsg" (see https://luarocks.org/modules/osch/mtmsg) but "mtint" itself does
not depend on these modules and you can use other modules for this if you want.

Best regards,
Oliver