Howto process X11 events async'ly

Robert Shearman rob at
Sat Jan 22 11:23:31 CST 2005

Peter Quiring wrote:

>I'm developing my own library and I'm in the process of porting it over in 
>Linux.  I can get X11 to display the graphics and process events, but the 
>only problem I have is with processing events without wasting CPU power.
>How do you process events without constantly polling?  What I've done is 
>create a new thread that will call XNextEvent() which will block if there 
>are no events waiting and this seems to work fine.  Except if the main 
>thread wants to use the Window it too will deadlock until XNextEvent() 
>returns an event.  I've even tried using XPutBackEvent() to wake-up the 
>other thread, but it looks like X11 is not smart enough to handle this. 
>Looking at Wine source it looks like they too have given up on X11 and do 
>sync themselves.

Yes, we have had to be very careful with locking around X11 calls, 
because we have seen some bugs in XFree86 in the past that have caused 
strange crashes when two threads call certain X functions at the same time.

>But then how do you know when new events are waiting without constantly 
>polling ?????  I've looked thru wine source but can't follow it.

It looks like we do the equivalent of select on the result from 
ConnectionNumber. I have no idea what this is, but hopefully you might. 
Grep the sources for "display_fd".

>Thanks in advance.  I've asked this question many times @ but 
>no one there understands the problem.


More information about the wine-devel mailing list