2013-03-20 103 views
0
for(unsigned int mBlock = 0; mBlock < coords.size(); mBlock++) 
{ 
    WidgetType widgetType; 
    height = macBlockWidth + coords[mBlock].y; 
    width = macBlockHeight + coords[mBlock].x; 

    macBlockParent = new QWidget; 
    cooefsLink = new QPushButton(macBlockParent); 
    macBlock = new QWidget(macBlockParent); 
    widgetType.widget = macBlock; 
    widgetType.type = (macBlocks[mBlock][2] != 'S') 
         ? (macBlocks[mBlock][0]) : (macBlocks[mBlock][2]); 
    blockWidgetTypes.push_back(widgetType); 

    connect(cooefsLink, SIGNAL(released()), 
            this, SLOT(showCoefficients())); 
    buttonSignals[cooefsLink] = mBlock; 

    constructMotionVector(mBlock); 
    macBlockLayout->addWidget(macBlockParent, height - 16, width - 16); 
    styleMacroBlocks(mBlock); 
} 

我可以做一个函数出于这个for循环,我可以通过将它分成两个不同的for循环同时在矢量上运行的平行操作。其中一个在前半部分工作,第二个在后半部分工作。因此,例如我可以线程这个for循环

线程1

for(unsigned int mBlock = 0; mBlock < coords.size()/2; mBlock++) 
{ 
    WidgetType widgetType; 
    height = macBlockWidth + coords[mBlock].y; 
    width = macBlockHeight + coords[mBlock].x; 

    macBlockParent = new QWidget; 
    cooefsLink = new QPushButton(macBlockParent); 
    macBlock = new QWidget(macBlockParent); 
    widgetType.widget = macBlock; 
    widgetType.type = (macBlocks[mBlock][2] != 'S') 
         ? (macBlocks[mBlock][0]) : (macBlocks[mBlock][2]); 
    blockWidgetTypes.push_back(widgetType); 

    connect(cooefsLink, SIGNAL(released()), 
            this, SLOT(showCoefficients())); 
    buttonSignals[cooefsLink] = mBlock; 

    constructMotionVector(mBlock); 
    macBlockLayout->addWidget(macBlockParent, height - 16, width - 16); 
    styleMacroBlocks(mBlock); 
} 

线程2

for(unsigned int mBlock = coords.size()/2; mBlock < coords.size(); mBlock++) 
{ 
    WidgetType widgetType; 
    height = macBlockWidth + coords[mBlock].y; 
    width = macBlockHeight + coords[mBlock].x; 

    macBlockParent = new QWidget; 
    cooefsLink = new QPushButton(macBlockParent); 
    macBlock = new QWidget(macBlockParent); 
    widgetType.widget = macBlock; 
    widgetType.type = (macBlocks[mBlock][2] != 'S') 
         ? (macBlocks[mBlock][0]) : (macBlocks[mBlock][2]); 
    blockWidgetTypes.push_back(widgetType); 

    connect(cooefsLink, SIGNAL(released()), 
            this, SLOT(showCoefficients())); 
    buttonSignals[cooefsLink] = mBlock; 

    constructMotionVector(mBlock); 
    macBlockLayout->addWidget(macBlockParent, height - 16, width - 16); 
    styleMacroBlocks(mBlock); 
} 

由于其对我的系统真正的瓶颈,我公司只使用一个CPU,并注意它的麻杏指出,中央处理器。任何帮助将非常感谢。

+0

另外我应该提到我知道我所提出的简单例子不是非常安全的,例如我需要两个不同的macBlockParent指针,例如macBlockParentT1和macBlockParentT2。 – 2013-03-20 21:22:47

+0

你有没有分析你的代码,看看它是吃什么CPU?我不熟悉QT,所以我在代码中看到的是一些小部件/控件的创建 - 这不应该是瓶颈,对吧?它究竟在哪里? – us2012 2013-03-20 22:21:53

+0

瓶颈在for循环中创建小部件,在较小分辨率的视频上它的zippy,但我现在正在研究更大的res,并且需要很长时间来构建框架。 – 2013-03-20 22:26:50

回答

0

嗯......如果你有这样的结构:blockWidgetTypes.push_back(widgetType);这两个线程,它看起来非常危险的多线程执行。

+0

我可以把它拿出来,我知道这很危险,但假设这不是一个问题,我最好怎么处理呢? – 2013-03-20 21:59:42

+0

@DavidTr,我在这里看不到任何简单的解决方案。 – qehgt 2013-03-20 22:10:15

+0

好的,谢谢,这真的是造成我的系统瓶颈。 – 2013-03-20 22:13:32