[Xcb] How to wake up the GUI thread in XCB?

Uli Schlachter psychon at znc.in
Wed May 20 23:18:24 PDT 2015


Am 20.05.2015 um 18:49 schrieb Peter Harris:
> On 2015-05-19 18:09, Cosmin Apreutesei wrote:
>> I have another question, this time I'm not sure I can get away without
>> threads. I would need something like xcb_wait_for_event(timeout). It
>> would make implementing timers trivial. But I guess
>> xcb_wait_for_event() doesn't use select() internally on a NULL
>> connection, so it's not as simple as just exposing the timeout arg
>> from that, right? Is that why I don't find an API for that, or is it
>> more complicated than that?
> 
> Generally if you want anything fancy (selecting on multiple file
> descriptors, custom timeout, etc) you would
> xcb_poll_for_queued_event()[1] until it returns NULL, then
> xcb_get_file_descriptor() and do your own select/poll/epoll/etc.

However, this only works reliably in single-threaded code. When threads are
involved, another thread could cause xcb to read events from the connection and
queue them internally between your loop "until it returns NULL" and your own
select/poll/epoll.

To quote again Qt's solution:
Qt starts a thread whose exclusive job is to call xcb_wait_for_event() in a
loop. Whenever it gets an event[0], it hands it to the main thread[1].

When you are threaded anyway, what problem is an extra thread? :-)

Uli


[0]: Technically, whenever it gets an event it also gets all queued events and
hands all these events to the main thread in one batch. This is for performance
reasons, but I guess that doesn't matter much.

[1]: "Handing to the main thread" means: there is a shared linked list protected
by a mutex. The mutex is taken, all events are appended to the list (by using
poll_for_queued_event()), the mutex is released and then the main thread is
woken up via a signal. I guess waking up could also be done via a pipe that the
main thread includes in its select()...
-- 
"Do you know that books smell like nutmeg or some spice from a foreign land?"
                                                  -- Faber in Fahrenheit 451


More information about the Xcb mailing list