Problem with Xtoolkit widgets together with Xlib drawing in threadedapplication - Xwindows

This is a discussion on Problem with Xtoolkit widgets together with Xlib drawing in threadedapplication - Xwindows ; Hello, I am developping an application, which measures analog data using PC soundcard and among other things it displays some real-time plots of them. OS is Linux 2.6, glibc6, pthreads, XFree86 version: 4.3.0.1. I am using X-toolkit widgets and the ...

+ Reply to Thread
Results 1 to 6 of 6

Thread: Problem with Xtoolkit widgets together with Xlib drawing in threadedapplication

  1. Problem with Xtoolkit widgets together with Xlib drawing in threadedapplication

    Hello,

    I am developping an application, which measures analog data using
    PC soundcard and among other things it displays some real-time plots
    of them. OS is Linux 2.6, glibc6, pthreads, XFree86 version: 4.3.0.1.

    I am using X-toolkit widgets and the class Core as a drawing canvas
    for signal plots. I plot using Xlib XLineDraw. Plotting of data is
    done in a thread, while XtAppMainLoop runs in another one. At the
    beginning, XInitThreads is called and then
    XtToolkitInitializeThreads.

    Each sequence of Xlib drawing (actually, XLineDraw calls only) is
    preceded by XLockDisplay and followed by XUnlockDisplay call.
    (Synchronization between expose and configure event handler and
    plotting thread is done by pthread mutex, but this is not essential
    for my question, I suppose.)

    * The problem:
    sometimes, mainly if a lot of resize (=>redraw) events is generated
    by messing with app. window, an Xlib error like BadRequest or "async
    reply" is received. Where is the problem?

    I suppose, if the Xtoolkit functions wouldn't call X[Un]LockDisplay
    before they call Xlib, there could be a race-condition of my
    XLockDisplay/XDrawLine..../XUnlockDisplay going in the middle of
    Xtoolkit=>Xlib action.

    Is this true? How can I solve the problem?


    Thank you very much for your help.
    Best regards,
    Marek P.


  2. Re: Problem with Xtoolkit widgets together with Xlib drawing in threaded application


    "Marek Peca" wrote in message
    news:Pine.LNX.4.10.10603151739560.22489-100000@tynska.cuni.cz...
    > Hello,
    >
    > I am developping an application, which measures analog data using
    > PC soundcard and among other things it displays some real-time plots
    > of them. OS is Linux 2.6, glibc6, pthreads, XFree86 version: 4.3.0.1.
    >
    > I am using X-toolkit widgets and the class Core as a drawing canvas
    > for signal plots. I plot using Xlib XLineDraw. Plotting of data is
    > done in a thread, while XtAppMainLoop runs in another one. At the
    > beginning, XInitThreads is called and then
    > XtToolkitInitializeThreads.


    All X calls must be made in the XtAppMainLoop thread.


    > Thank you very much for your help.
    > Best regards,
    > Marek P.
    >

    --
    Fred L. Kleinschmidt
    Boeing Associate Technical Fellow
    Technical Architect, Software Reuse Project



  3. Re: Problem with Xtoolkit widgets together with Xlib drawing inthreaded application

    Dear Fred,

    > > I am using X-toolkit widgets and the class Core as a drawing canvas
    > > for signal plots. I plot using Xlib XLineDraw. Plotting of data is
    > > done in a thread, while XtAppMainLoop runs in another one. At the
    > > beginning, XInitThreads is called and then
    > > XtToolkitInitializeThreads.

    >
    > All X calls must be made in the XtAppMainLoop thread.


    I can believe you, but can you tell me the reason?
    If both libraries are thread-safe (at least according to
    documentation)?

    Thank you.

    Best regards,
    Marek P.


  4. Re: Problem with Xtoolkit widgets together with Xlib drawing in threaded application


    "Marek Peca" wrote in message
    news:Pine.LNX.4.10.10603161848120.26551-100000@tynska.cuni.cz...
    > Dear Fred,
    >
    >> > I am using X-toolkit widgets and the class Core as a drawing canvas
    >> > for signal plots. I plot using Xlib XLineDraw. Plotting of data is
    >> > done in a thread, while XtAppMainLoop runs in another one. At the
    >> > beginning, XInitThreads is called and then
    >> > XtToolkitInitializeThreads.

    >>
    >> All X calls must be made in the XtAppMainLoop thread.

    >
    > I can believe you, but can you tell me the reason?
    > If both libraries are thread-safe (at least according to
    > documentation)?
    >
    > Thank you.
    >
    > Best regards,
    > Marek P.
    >

    It isn't safe to make X calls independently in two threads.
    Consider pressing and releasing a mouse button in a Button widget.
    What should happen when the mouse is pressed is that the
    Button should be drawn as armed. If the mouse is dragged outside
    the Button (mouse still down), the Button should be redrawn
    as unarmed. If dragged back in, armed.

    When mouse is released, Button is drawn as unarmed and if
    the release was inside the Button, the Button's activate callbacks are
    called.

    But if two threads are involved, the arming/disarming can get
    out-of-synch.

    Suppose on arm, you clear an area and draw a line somewhere in it.
    And on release you clear it again and draw a different line.

    So, click the mouse. Which line shows up?
    If you are doing things in different threads, there is no
    guarantee which line will show up, if any at all!
    Lets say you get the down event, then in Thread A you draw line A.
    When you get the up event, Thread B draws line B.
    But which thread finishes execution first, if you just click the mouse?
    --
    Fred L. Kleinschmidt
    Boeing Associate Technical Fellow
    Technical Architect, Software Reuse Project




  5. Re: Problem with Xtoolkit widgets together with Xlib drawing in threaded application

    Dear Fred,

    thank you for your answer. However, I believe, that explanation, which
    you gave me, does not apply in my problem. I have (need) one thread
    servicing GUI and another one, which updates plotted data in a window.
    There is no other interaction. Because the data are born in hardware
    using highly-prioritized thread and then they are passed to
    lower-priority thread, it seems to me to be good to draw from this
    thread directly and not to pass the data through additional chain of
    signals/file descriptors... (I know, I can use XtAppAddInput or
    XtAppAddSignal instead of using threads; I cannot use WorkProc, it
    gives 100% of load in my application).

    I hope I have found the answer in using XtAppLock call, it seems to me
    it does exactly what I want in protecting Xlib against Xtoolkit call
    interaction.

    I wonder, how can you say "It isn't safe to make X calls independently
    in two threads." in general. Why, then, are that Xlib/Xtoolkit thread
    support functions implemented? In example, which you gave us, it is
    true, but in my app I hope the use of threads is a correct way.


    Best regards,
    Marek Peca.


  6. Re: Problem with Xtoolkit widgets together with Xlib drawing in threadedapplication

    Hello Marek,

    You can have some fun reading old slashdot stuff on your question...
    http://ask.slashdot.org/article.pl?sid=03/01/11/0043207

    There is no performance penalty in concentrating all the
    X drawing in one thread/process. You only have one X server,
    anyway. Use shared memory to store the incoming data to be drawn.
    Use signals to tell the GUI process that data needs to be drawn.
    The signal function in GUI process should only set a flag that
    data is in, and the actual drawing should be done once
    XtMainLoop gets around to process your callbacks.
    And use XDrawLines instead of XDrawLine ;-)

    If you insist on doing it your way, be prepared to deal
    with the trouble it creates.

    Best regards,

    Dušan Peterc
    http://www.arahne.si

+ Reply to Thread