Thanks for your explanation, Eric, and thanks to Antonio, too, for his C
example.
So far, I do not use any Lua-level threading library, and I am not
convinced I need one. I am binding a bunch of C++ code to Lua using
Sol2, and while the C++ code uses multiple threads, the Lua code is
supposed to only ever deal with a single thread that runs the GUI and
its main loop.
Sometimes events happen in C++-Land that require an update of a GUI
element. I figured that the most sensible way of doing this is to call
IupPostMessage from C++ territory, in whatever thread context this
happens to be, and thus cause a Lua callback to be called in the context
of the GUI main loop the next time it comes around.
The C++ code resides in a runtime-loaded Lua module (or, actually,
several of them), and IUP is also loaded dynamically. So far, the
modules don't know anything about each other, and I'd like to keep it
this way.
So I need a way to wrap IupPostMessage into a C++ callback that I can
pass to a function exported by one of the C++ modules. And I need to
package the IUP handle to use along with it. That's what I'm currently
struggling with.
For example, assume a C++ function that's exported by a dynamically
loaded module that looks like this:
void setCallback(std::function<void()> f);
Sol2 would allow me to bind this to Lua, and for use with IUP I would
like to be able to call this in approximately the following way from
Lua, in the context of the GUI main loop:
cb = makeIupCallback(ih, s, i, d, p)
setCallback(cb)
The first line would create a Lua object that packages everything needed
for the callback in a C++ std::function. The second line installs the
callback in the C++ module, so that whenever the appropriate event
happens, the module invokes the stored callback. This callback will
retrieve the stored parameters packaged inside the std::function object,
and call IupPostMessage with them as the arguments.
I just need to write makeIupCallback :-)
And I guess I need some further help with that.
My current suspicion is that I have got to write yet another C++ module
that links with the IUP library so that it can call IupPostMessage, and
use Sol2 to bind that to Lua. But that sounds like an oversized solution
to me. Perhaps there's a more lightweight way that keeps things properly
separate from each other, and I don't see it yet.
Cheers, and thanks again!
Stefan
Am 26/05/2022 um 00:06 schrieb Eric Wing:
On 5/24/22, Stefan Heinzmann <stefan_heinzm...@gmx.de> wrote:
Hello list,
The documentation mentions in passing that IupPostMessage is expected to
be thread safe, which I take to mean that it can be called from any
other thread than the thread running the IUP main loop, without needing
any synchronization.
I would like to do that with Lua rather than C, and I wonder how it is
done in practice. I would appreciate a tutorial example that shows how I
can invoke iup.PostMessage from other threads without risking threading
mishaps.
Thanks!
Stefan
I helped implement IupPostMessage. In simplified terms, the way it
works is that it uses the underlying OS APIs to post a message to the
native event loop queue. Then back on the main thread, when the native
event comes up at the top of the queue for handling, IUP then invokes
your callback function (which is now being invoked/run on the main UI
thread).
So simply, you can call IupPostMessage from any thread, and the
callback will always happen some time later on the main thread.
Additionally, if the message you post is just primitive types (in C,
e.g. int, double), then there is no locking needed to protect the data
since the posted message is a copy. But if you pass pointers through
IupPostMessage and modify things, then you still need to give
consideration to what happens if multiple threads are modifying the
shared data.
In Lua, it should be the same thing. IupPostMessage won't directly
cause any threading issues, but ultimately, any threading mishaps will
be caused by mishaps with shared state across your different threads,
which is probably more likely going to be the result of what's going
on in the rest of your program and not IupPostMessage itself. (For
example, while Lua does not care what thread it is run on, it is not
safe to concurrently run the same Lua state on multiple threads at the
same time, so whatever mechanism you use to multithread with Lua needs
to deal with this.)
-Eric
_______________________________________________
Iup-users mailing list
Iup-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/iup-users
_______________________________________________
Iup-users mailing list
Iup-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/iup-users