Slashdot is powered by your submissions, so send in your scoop

 



Forgot your password?
typodupeerror
×
X GUI

Why Isn't X11 Thread-Safe? 44

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?
This discussion has been archived. No new comments can be posted.

Why Isn't X11 Thread-Safe?

Comments Filter:
  • What I do (Score:5, Interesting)

    by Procyon101 ( 61366 ) on Thursday January 16, 2003 @05:06PM (#5097426) Journal
    What I do is create a GUI thread which handles all interface to the GUI. All of my other threads then fire their events to that thread through an interface that IS threadsafe. The GUI thread then synchronously handles the GUI events. This abstraction away from your actual GUI API allows you to more easily port your app to another platform because all of your X11 specific code is contained in one place.
  • by JohnFluxx ( 413620 ) on Thursday January 16, 2003 @07:29PM (#5098497)
    Why do you think X sucks?

    It is a very impressive protocol - just consider how long it has been around, and you can do just about anything without breaking it.
    How many other protocols are there that can boast that?

    What can't X do?
  • Re:What I do (Score:4, Interesting)

    by mike_sucks ( 55259 ) on Thursday January 16, 2003 @08:23PM (#5098878) Homepage

    Yeah, I think having a single GUI thread per app is pretty much standard for all the environments I've programmed for.

    There's absolutely no need for a display system like X to allow multiple application threads to concurrently recieve events and/or update the application's display. It would just add unnesessary complexity, making it harder to debug and maintain.

    Also, having a single GUI thread is a good design pattern. All GUI work is handled by one thead, all application logic is (potentially) handled by other threads. The delineation between the application's UI and logic makes it much easier to maintain.

    So yeah, blackcoot shouldn't complain that X is broken, I'd say whatever app {s}he is writing needs to get fixed.

  • by nathanh ( 1214 ) on Thursday January 16, 2003 @08:44PM (#5099000) Homepage
    Xlib was made thread safe in the X11R6 [uni-bremen.de] release in 1994, but only if you initialize the locks it needs to do it properly via XInitThreads [xfree86.org].

    Xlib is thread safe but X11 is not. If your application has multiple threads then they must compete for the "display lock" with XLockDisplay and XUnlockDisplay. In other words, Xlib is used to solve the thread unsafety of X11. It's a library space (Xlib) solution to a protocol (X11) limitation.

    So while you are right - Xlib is thread safe - you weren't answering the original question which is Why Isn't X11 Thread Safe? The answer is that it's not possible. He might as well ask why HTTP isn't thread safe, or why SMTP isn't thread safe. Imagine muxing two HTTP requests into a single request! It'd look like this:

    GET /GET /index.htpage.asmlp HTTHTPTP/1./1.00

    It's not an issue for the protocol to solve. This is why it's a non-problem. The questioner is simply confused. You don't make a protocol thread safe. You make the library calls to the protocol thread safe, and as you point out this has been solved with XFree86 for almost a decade.

  • Qt is thread safe (Score:2, Interesting)

    by frezeal ( 133571 ) on Thursday January 16, 2003 @08:46PM (#5099008) Homepage
    If you link against libqt-mt, you can post events outside of the main event thread. You still shouldn't call methods directly on QtWidgets. But the link should answer all of your questions and solve your problem.

    http://doc.trolltech.com/3.0/threads.html
  • Don't Bother (Score:2, Interesting)

    by Markus Registrada ( 642224 ) on Friday January 17, 2003 @12:28AM (#5099919)
    There's no reason for the X wire protocol to be "thread-safe" (i.e. atomic/transactional). There's not even any need to use locks in Xlib.

    A multithreaded program may simply open several connections to the X server, and operate independent UIs. One thread might operate a display window, while another operates a dialog box or progress bar. As far as X is concerned, it's talking to different programs.

    Don't go looking for complicated solutions when simple ones are right at hand.

Those who can, do; those who can't, write. Those who can't write work for the Bell Labs Record.

Working...