XPending cycle is making CPU 100%

↘锁芯ラ 提交于 2020-01-03 19:06:10

问题


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.


回答1:


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);


来源:https://stackoverflow.com/questions/19659486/xpending-cycle-is-making-cpu-100

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!