我需要使用梯形规则和多线程计算积分。
我正在使用我使用java 池线程示例编写的池线程。
#ifndef POOL_H
#define POOL_H
#include <QObject>
#include <QThread>
#include <QWaitCondition>
#include <QMutex>
#include <QQueue>
#include "poolworker.h"
#include "segment.h"
class Segment;
class PoolWorker;
class Pool: public QObject
{
Q_OBJECT
public:
explicit Pool(QObject *parent = 0);
Pool(int nThreads);
void execute(Segment *s);
static QWaitCondition con;
static QMutex poolMutex;
static QQueue<Segment*> segmentQueue;
private:
int nThreads;
QVector<PoolWorker*> workers;
};
#endif // POOL_H
#include "pool.h"
QWaitCondition Pool::con;
QQueue<Segment*> Pool::segmentQueue;
QMutex Pool::poolMutex;
Pool::Pool(QObject *parent) :
QObject(parent)
{
}
Pool::Pool(int nThreads)
{
this->nThreads = nThreads;
for (int i = 0; i < nThreads; i++)
{
workers.push_back(new PoolWorker());
workers[i]->start();
}
}
void Pool::execute(Segment *s)
{
poolMutex.lock();
segmentQueue.enqueue(s);
con.wakeOne();
poolMutex.unlock();
}
#ifndef POOLWORKER_H
#define POOLWORKER_H
#include <QThread>
#include <QMutex>
#include "segment.h"
#include "pool.h"
class PoolWorker : public QThread
{
Q_OBJECT
public:
explicit PoolWorker(QObject *parent = 0);
void run();
static QMutex mutex;
signals:
public slots:
private:
};
#endif // POOLWORKER_H
#include "poolworker.h"
QMutex PoolWorker::mutex;
PoolWorker::PoolWorker(QObject *parent) :
QThread(parent)
{
}
void PoolWorker::run()
{
Segment *temp;
forever
{
mutex.lock();
while(Pool::segmentQueue.isEmpty())
{
Pool::con.wait(&mutex);
}
temp = Pool::segmentQueue.dequeue();
mutex.unlock();
temp->doWork();
}
}
每个区间都被放入一个容器“Segment”中,该容器也计算积分。
Sab = 0.5*(b-a)*(f(a)+f(b))
m = (a+b)/2.0
Sam = 0.5*(m-a)*(f(a)+f(m))
Smb = 0.5*(b-m)*(f(b)+f(m))
如果 和 之间的差Sab
小于Sam+Smb
,Eps
那么我Sab
使用 来添加积分和Manager::addSum
。如果它不低,我对am
和执行相同的算法mb
。等等
#ifndef SEGMENT_H
#define SEGMENT_H
#include <QObject>
#include <cmath>
#include "manager.h"
#include <QDebug>
class Segment : public QObject
{
Q_OBJECT
private:
double a,b,Sab,Sam,Smb,m,Eps;
double f(double x);
public:
explicit Segment(QObject *parent = 0);
Segment(double a, double b);
void doWork();
signals:
public slots:
};
#endif // SEGMENT_H
#include "segment.h"
Segment::Segment(QObject *parent) :
QObject(parent)
{
}
Segment::Segment(double a, double b)
{
this->a = a;
this->b = b;
Eps = 0.001;
}
void Segment::doWork()
{
Sab = 0.5*(b-a)*(f(a)+f(b));
m = (a+b)/2.0;
Sam = 0.5*(m-a)*(f(a)+f(m));
Smb = 0.5*(b-m)*(f(b)+f(m));
if (fabs(Sab - (Sam + Smb)) <= Eps)
{
Manager::addSum(Sab);
qDebug() << "Reached Eps on interval a= " << a << ",b = " << b
<< ", return S+= " << Sab;
Manager::inc();
}
else
{
Manager::threadPool->execute(new Segment(a,m));
Manager::threadPool->execute(new Segment(m,b));
}
}
double Segment::f(double x)
{
return pow(x,3.0) - 4.0*pow(x,2.0) + 6.0*x - 24.0;
}
Manager 类将所有内容联系起来:它创建池,包含总和并通过在第一个间隔调用池上的 execute 来开始计算。它还有一个用于调试目的的计数器。
#ifndef MANAGER_H
#define MANAGER_H
#include <QObject>
#include <QThread>
#include <QQueue>
#include <QVector>
#include "segment.h"
#include "pool.h"
class Pool;
class Manager : public QObject
{
Q_OBJECT
private:
static double sum;
static int i;
static QMutex mutex;
public:
explicit Manager(QObject *parent = 0);
static Pool *threadPool;
static void addSum(double add);
static void inc();
double viewSum();
int viewCount();
void doSetup(QThread &thread);
signals:
public slots:
void doWork();
};
#endif // MANAGER_H
#include "manager.h"
double Manager::sum = 0;
int Manager::i = 0;
Pool* Manager::threadPool = new Pool(10);
QMutex Manager::mutex;
Manager::Manager(QObject *parent) :
QObject(parent)
{
}
void Manager::addSum(double add)
{
mutex.lock();
sum += add;
mutex.unlock();
}
void Manager::inc()
{
i++;
}
double Manager::viewSum()
{
return sum;
}
int Manager::viewCount()
{
return i;
}
void Manager::doSetup(QThread &thread)
{
connect(&thread,SIGNAL(started()),this,SLOT(doWork()));
}
void Manager::doWork()
{
threadPool->execute(new Segment(4.5,12.0));
}
在 main 中,我创建了管理器、管理器的线程并显示结果。
#include <QCoreApplication>
#include <QDebug>
#include <QThread>
#include <QTimer>
#include "manager.h"
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
Manager man;
QThread manThread;
man.doSetup(manThread);
man.moveToThread(&manThread);
manThread.start();
manThread.wait(2500);
qDebug() << "integrate(x^3 - 4*x^2 + 6*x - 24) from 4.5 to 12.0 = "
<< man.viewSum();
qDebug() << "i =" << man.viewCount();
manThread.quit();
QTimer::singleShot(1000, &a, SLOT(quit()));
return a.exec();
}
它在大约一半的时间内正确计算了积分。另一半我得到的数字比预期的要大(这会有所不同)。当我得到一个更大的数字时,我注意到一些间隔被计算了两次。如果我没记错的话,我已经使代码线程安全,所以我不明白这是怎么发生的。我对多线程编程很陌生,所以我可能对互斥锁做错了什么?或者也许我从 Java 池的过渡是错误的?
另一件事是在 main.cpp 我不确定如何正确显示结果,因为我不知道积分何时完成计算。我在包含管理器的线程上使用了 wait(2500) 函数,但这不是一个真正好的方法,因为计算时间可能因不同的 PC 和不同的函数而异。
提前感谢您提供的任何帮助。