Note that there are some explanatory texts on larger screens.

plurals
  1. POThreading problems with GTK
    primarykey
    data
    text
    <p>I'm building a fairly simple <a href="https://github.com/jonhoo/mktrayicon">C application</a> using GTK, but have to perform some blocking IO which will trigger updates to the GUI. In order to do this, I start a new <code>pthread</code> right before <code>gtk_main()</code> as such:</p> <pre class="lang-c prettyprint-override"><code>/* global variables */ GMainContext *mainc; /* local variables */ FILE *fifo; pthread_t reader; /* main() */ mainc = g_main_context_default(); pthread_create(&amp;reader, NULL, watch_fifo, argv[argc-1]); gtk_main(); </code></pre> <p>When the <code>pthread</code> reads some data, it updates the GUI like so:</p> <pre class="lang-c prettyprint-override"><code>g_main_context_invoke(mainc, set_icon, param); </code></pre> <p>Where <code>set_icon</code> is </p> <pre class="lang-c prettyprint-override"><code>gboolean set_icon(gpointer data) { char *p = (char*)data; gtk_status_icon_set_from_icon_name(icon, p); return FALSE; } </code></pre> <p>This all works most of the time, but every now and again I get this curious error message:</p> <pre> [xcb] Unknown sequence number while processing queue [xcb] Most likely this is a multi-threaded client and XInitThreads has not been called [xcb] Aborting, sorry about that. mktrayicon: xcb_io.c:274: poll_for_event: Assertion `!xcb_xlib_threads_sequence_lost' failed. </pre> <p>I thought the whole point of using <code>g_main_context_invoke</code> was to avoid issues with threads? Doing a bit of Googling, I came across <code>gdk_threads_init</code>, <code>gdk_threads_enter</code> and friends, but they all seem to be deprecated? I know the GTK documentation says that all GUI updaes should be performed on the main thread, but this does not combine all that well with blocking IO, and I'd prefer not to have to construct some complex communication mechanism between the threads.</p> <p>And so, my question is, how should I correctly deal with this?</p> <p>EDIT: The full code can be seen <a href="https://github.com/Jonhoo/mktrayicon/blob/master/mktrayicon.c">here</a> EDIT2: As an update based on @ptomato's answer, I've moved to <code>GThread</code>s and using <code>gdk_threads_add_idle()</code> as seen in <a href="https://github.com/Jonhoo/mktrayicon/commit/e62ede1cf0e863e0e9173d15f46a01bd6536fa11">this</a> commit, but the problem is still present.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload