Slashdot Mirror


Why Isn't X11 Thread-Safe?

blackcoot asks: "I've just spent a couple very frustrating days trying to figure out what 'unexpected async reply' means and fixing it. The problem is a result of the fact that X11 simply isn't designed to handle events from more than one thread at a time. Why? Given that more and more often, people are writing multi-threaded GUI applications, are there fundamental design decisions in X11 that make dealing with receiving events from multiple threads simultaneously, impossible? Or was the protocol never designed to handle concurrent updates? More to the point, is there an easy way in Qt (short of deriving a new widget for every widget and overriding it's paintEvent to lock the library first, paint, then unlock as Trolltech's docs seem to suggest) to make this problem go away?" I'm not sure if things have been done in recent revisions of XFree to fix this problem, but this message, from February of last year, might help some of you out that are suffering from this problem. Any ideas if this problem has been fixed in recent versions of XFree?

2 of 44 comments (clear)

  1. Re:don't use threads! by michaelggreer · · Score: 3, Insightful

    True, if all the app does is GUI. Perhaps, though, when you fire events in the GUI, something occurs. Surely you would not want the GUI to wait on the result of that fired procedure: you would execute it in a seperate thread. Or, maybe the GUI is not entirely static (a movie clip is showing, for instance). You would not want the event updates fighting with the movie playback: you would run them in seperate threads.

  2. Re:don't use threads! by Euphonious+Coward · · Score: 3, Insightful
    michaelggreer wrote: "You would not want the event updates fighting with the movie playback"

    If some events should have higher priority than others, your GUI thread should schedule them properly. It would be a grave design error for the X server to have to know about threads in the client, and their relative priority. However, finding yourself implementing a priority scheduler in user-space is often an indication that you have made a wrong turn in your architecture. Scheduling is what OSes are for.

    As an alternative, the client is free to open multiple connections to the X server, and operate independent UIs through them. As far as the X server is concerned, it's talking to independent clients, something it is very good at.