0

So I am writing a basic Gtk based text editor, and I'm trying to communicate to the user that a file they are editing on disk has changed - I have already got a basic way to submit watchers to an inotify monitoring thread - so I tried just showing a basic prompt and I got an assortment of errors from xcb, seeming to point to cross-thread window creation causing issues.

If I had control of the main thread I would just use a pipe or what-have-you to indicate that a prompt should be created, but gtk_main occupies the main thread.

My current idea is to use g_main_context_set_poll_func() to redirect poll/signal handling to a user function, then send a SIGUSR from the secondary thread to indicate that a pipe has details in it - using this instead of g_source_add_unix_fd() since the docs say

Do not call this API on a GSource that you did not create.

(If it's actually fine to then I'll just use that and a regular pipe)

So my question is - is that the intended method to handle cross-thread communication from a secondary thread to the gtk/glib main thread (i.e. hooking into g_main_context_set_poll_func), or is there a more formal interface for it?

If I've missed any details or context I apologise - and thanks in advance!

(In case it matters, here are some details about the versions of gtk/xcb according to pacman)

gtk3-1:3.24.29-1

libxcb-1.14-1

Emily-TTG
  • 531
  • 3
  • 18
  • 1
    I'm not a Glib expert at all, but looking at the docs, wouldn't a custom GSource that reads a suitably locked cross thread queue or similar do? – AKX Jun 03 '21 at 19:08
  • @AKX How `GSource`s didn't come up in my searches I do not know - the power of fresh eyes prevails IG - ty! – Emily-TTG Jun 03 '21 at 19:14
  • To send a message between threads using GLib, use `g_idle_add()` to add an idle source to the `GMainContext` in the main thread. Return `G_SOURCE_REMOVE` from the idle source callback so that it’s invoked only once. However, you may want to instead look at `GFileMonitor` from GIO, which can do the file monitoring for you, without you having to write it in a separate thread yourself. – Philip Withnall Jun 03 '21 at 20:07
  • @PhilipWithnall ty - in the end I found from AKXs mention of `GSource` the following ``` GIOChannel* channel = g_io_channel_unix_new(fd); g_io_add_watch(channel, G_IO_IN, callback, NULL); ``` – Emily-TTG Jun 04 '21 at 17:50

0 Answers0