我可以将其换成循环

时间:2013-03-20 21:21:18

标签: c++ multithreading qt

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循环来同时操作,这两个循环同时在向量上运行。一个工作在上半部分,第二个工作在后半部分。例如,

主题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并且最大限度地利用了CPU。任何帮助都会非常感谢。

1 个答案:

答案 0 :(得分:0)

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