XPending cycle is making CPU 100%
Asked Answered
B

1

2

Good day!

I have a little bit of troubles making a xlib project. Here is the structure of my project:

[ Init ]
[ Making some stuff ]
[ Creating a timer thread (see code below) ]
[ Main cycle (see code below) ]

When the user presses any button, I set the flag in the thread to true-like value and it starts to send CustomMessage to the window every n time.

while (warehouse.destroyflag != SML_DEAD)
{
    if (XPending(warehouse.display))
    {
        XNextEvent(warehouse.display, &event);

But there is a bit of problems here. With the current realisation of the main cycle I have about 100% CPU load. But when I remove the XPending line from the code, the load is about to be 0%. But in that case I don't have correct CustomMessage arriving from the another thread.

I have found the sample code of Xlib program and compiled it. It has the same problem, the CPU load is about 100%. Here is the sample:

http://paste.bradleygill.com/index.php?paste_id=4897

Here is my thread's code: http://paste.bradleygill.com/index.php?paste_id=4898

And here is my cycle: http://paste.bradleygill.com/index.php?paste_id=4899

I read the GTK+ project code and found out that it has the very similar cycle, but I can't see that any of GTK+ applications have 100% CPU load because of that.

Thank you for any answer.

Boeotian answered 29/10, 2013 at 13:24 Comment(4)
Q: Does it behave the same if you change to if (XPending(warehouse.display) > 0) {...}?Capitulum
Yes, it stays the sameBoeotian
Just found out the solution. Edited the post, thank you!Boeotian
@AlexTiger Show the solution as an answer Do not edit the questionFideicommissary
E
2

Alex, I've pulled your answer out of your question and posted it here for future reference.

Change the loop to:

while (warehouse.destroyflag != SML_DEAD)
{
    while (XNextEvent(warehouse.display, &event) >= 0)
    {

and the thread code to:

XLockDisplay(warehouse.display);
{
     XSendEvent(warehouse.display,
                event.xclient.window,
                0, NoEventMask, &event);
     XFlush(warehouse.display);
}
XUnlockDisplay(warehouse.display);
Evening answered 10/12, 2013 at 20:20 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.