Reputation: 41
I have a Qt-Application written in C++. It's a generic Tool for testing hardware. The specific tests are defined in a lua-script.
In my script i have a function called 'RunTests()' which is called in a QThread in the Qt-Application. I put it into a QThread in order to prevent the script from freezing my application. Now there's another function in the lua script, called 'Interrupt()' which should be called sometimes by the Qt-Application during the tests. So now everytime I call this function 'Interrupt()' with lua_getglobal() my Qt-Application crashes.
How can I run those 2 Lua functions at the same time or how can I interrupt 'RunTests()' to call 'Interrupt()' and then move back to 'RunTests()'?
Upvotes: 3
Views: 300
Reputation: 41
I now solved the problem by using coroutines.
So when my two functions are:
function DataHandler(data)
buffer = buffer .. data
...
end
function Tests()
...
end
I added the following lines of code:
co_DataHandler = coroutine.create(DataHandler)
co_Tests = coroutine.create(Tests)
With this I am able to run DataHandler() within the same QThread while Tests() is doing some other stuff.
Upvotes: 0
Reputation: 17415
Your question boils down to "How can I interrupt a thread?". This is a common question and the answer is usually that you can't, but that there are other ways. The simple reason why you can't is that a thread might be in the middle of some operation that modifies global state, like a heap allocation. If you killed it in the middle, it would leave data structures in a temporarily invalid state and leave mutexes locked.
Two approaches come to mind that might work for you:
Upvotes: 1
Reputation: 72312
You cannot run concurrent scripts in the same Lua state.
You can run concurrent scripts in separate Lua states.
Upvotes: 2