# 一个线程池的例子（二）

def.h

#ifndef DEF_H
#define DEF_H

#include <QVector>
#include <math.h>
#include <QWaitCondition>
#include <QMutex>
#include <QDebug>

{
long m_StartValue;
long m_EndValue;
long m_MinResultValue;
long m_MaxResultValue;
__int64 m_SumResultValue;

{
m_StartValue = 0;
m_EndValue = 0;
m_MinResultValue = 0;
m_MaxResultValue = 0;
m_SumResultValue = 0;
}

{
if (this != &T)
{
m_StartValue = T.m_StartValue;
m_EndValue = T.m_EndValue;
m_MinResultValue = T.m_MinResultValue;
m_MaxResultValue = T.m_MaxResultValue;
m_SumResultValue = T.m_SumResultValue;
}

return *this;
}
};

static bool IsPrime(unsigned long n)
{
if (n == 2)
{
return true;
}

if (n % 2 == 0 || n < 2)
{
return false;
}

long tmp = (int)sqrt((double)n);
for (long i = 3; i <= tmp; i += 2)
{
if (n % i == 0)
{
return false;
}
}

return true;
}

{
long PrimesCount = 0;

for (long i = T.m_StartValue; i < T.m_EndValue; i++)
{
if (IsPrime(i))
{
PrimesCount++;

if (PrimesCount == 1)
{
T.m_MinResultValue = i;
}

T.m_SumResultValue = T.m_SumResultValue + (__int64)i;
}
}

PrimesCount = 0;
for (long i = T.m_EndValue - 1; i >= T.m_StartValue; i--)
{
if (IsPrime(i))
{
PrimesCount++;

if (PrimesCount == 1)
{
T.m_MaxResultValue = i;
break;
}
}
}

}

#endif

#ifndef WORKER_THREAD_H

#include "def.h"

#include <QObject>

{
Q_OBJECT

public:

protected:
void run();

};

#endif


#include "WorkerThread.h"

{
}

{
wait();
}

{
{
}

while(1)
{
{
break;
}

CalCulatePrime(T);
" Start: " << T.m_StartValue <<
" End: " << T.m_EndValue <<
" MinResult: " << T.m_MinResultValue <<
" MaxResult: " << T.m_MaxResultValue <<
" SumResult: " << T.m_SumResultValue;

{
break;
}

}

}


main.cpp

#include "WorkerThread.h"

#include <QtCore/QCoreApplication>

int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);

for (int i = 0; i < 8; i++)
{
}

for (int i = 0; i < 50; i++)
{
T.m_StartValue = i * 1000;
T.m_EndValue = (i + 1) * 1000 * 100;
}

{
}

for (int i = 0; i < 8; i++)
{