问题:
线程间总是完全独立毫无依赖嘛?
结论:
在特殊情况下,多线程的执行在时许上纯在依赖!
qDebug() << "begin";
QThread t;
t.start(); // 创建并启动子线程
t.wait(); // 等待子线程执行结束
class Calculator : public QThread
{
protected:
int m_result;
int m_begin;
int m_end;
void run()
{
qDebug() << objectName() << " begin";
for(int i=m_begin; i <= m_end; i++)
{
m_result += i;
}
msleep(20);
qDebug() << objectName() << " end";
}
public:
Calculator(int begin, int end)
{
m_result = 0;
m_begin = begin;
m_end = end;
}
void work()
{
run();
}
int result()
{
return m_result;
}
};
qDebug() << "mian begin";
Calculator c1(1,30);
Calculator c2(31,60);
Calculator c3(61,100);
c1.setObjectName("c1");
c2.setObjectName("c2");
c3.setObjectName("c3");
c1.start();
c2.start();
c3.start();
c1.wait();
c2.wait();
qDebug() << "Reult =" << c1.result() + c2.result() + c3.result();
qDebug() << "mian end";
注:本课程根据狄泰软件学院课程做的学习笔记