XPending周期使CPU 100% [英] XPending cycle is making CPU 100%

查看:107
本文介绍了XPending周期使CPU 100%的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

美好的一天!

我在制作xlib项目时遇到了一些麻烦.这是我的项目的结构:

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

当用户按下任何按钮时,我将线程中的标志设置为真值,并且它每隔n次开始向窗口发送CustomMessage.

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

但是这里有一些问题. 在当前实现主周期的情况下,我有大约100%的CPU负载.但是,当我从代码中删除XPending行时,负载大约为0%.但是在那种情况下,我没有从另一个线程到达的正确的CustomMessage.

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.

我找到了Xlib程序的示例代码并进行了编译.它有相同的问题,CPU负载约为100%.这是示例:

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

这是我的线程的代码: http://paste.bradleygill.com/index.php?paste_id=4898

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

这是我的周期: http://paste.bradleygill.com/index.php?paste_id=4899

我阅读了GTK +项目代码,发现它具有非常相似的周期,但是由于这个原因,我看不到任何GTK +应用程序具有100%的CPU负载.

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.

谢谢您的回答.

推荐答案

Alex,我已将您的答案从您的问题中删除,并将其张贴在此处以供将来参考.

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

将循环更改为:

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

这篇关于XPending周期使CPU 100%的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆