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

Reply via email to