- 算法策略
- 将资金优先借予资金需求较少的客户
- 应用场景
- 操作系统内核中的进程管理
- 数据库内核中的频繁事物管理
- Qt中的算法实现方案
- 使用多线程机制模拟客户和银行
- 银行优先分配资源给最小需求的客户
- 当客户的资源需求无法满足的时候
- 收回已分配的资源
- 强制结束线程
代码实现:
#include <QCoreApplication>
#include <QThread>
#include <QMutex>
#include <QList>
#include <QDebug>
class Customer : public QThread
{
protected:
int m_need;
volatile int m_current;
QMutex m_mutex;
bool m_toStop;
void run()
{
bool condition = false;
qDebug() << objectName() << "begin to apply money";
do
{
m_mutex.lock();
condition = (m_current < m_need);
m_mutex.unlock();
msleep(10);
}
while( condition && !m_toStop);
if(!m_toStop)
{
qDebug() << objectName() << "end (get enough money)";
}
}
public:
Customer(int current, int need)
{
m_current = current;
m_need = need;
m_toStop = false;
}
void addMoney(int m)
{
m_mutex.lock();
m_current += m;
m_mutex.unlock();
}
int backMoney()
{
int ret = 0;
m_mutex.lock();
ret = m_current;
m_current = 0;
m_mutex.unlock();
return ret;
}
int current()
{
int ret = 0;
m_mutex.lock();
ret = m_current;
m_mutex.unlock();
return ret;
}
void stop()
{
m_toStop = true;
}
int need()
{
return m_need;
}
};
class Bank : public QThread
{
protected:
QList<Customer*> m_list;
int m_total;
void run()
{
int index = -1;
qDebug() << objectName() << " begin: " << m_total;
do
{
index = -1;
for(int i=0; i<m_list.count(); i++)
{
if( m_list[i]->current() == m_list[i]->need() )
{
qDebug() << objectName() << " take back money from " << m_list[i]->objectName() << " " << m_list[i]->need();
m_total += m_list[i]->backMoney();
}
}
qDebug() << objectName() << " current: " << m_total;
int toGet = 0x00FFFFFF;
for(int i=0; i<m_list.count(); i++)
{
if( m_list[i]->isRunning() )
{
int tmp = m_list[i]->need() - m_list[i]->current();
if( toGet > tmp )
{
index = i;
toGet = tmp;
}
}
}
if( index >=0 )
{
if( toGet <= m_total )
{
qDebug() << objectName() << " give money to: " << m_list[index]->objectName();
m_total--;
m_list[index]->addMoney(1);
}
else
{
qDebug() << objectName() << " terminate(stop): " << m_list[index]->objectName();
m_total += m_list[index]->backMoney();
m_list[index]->stop();
}
}
sleep(1);
}
while( index >= 0 );
qDebug() << objectName() << " end: " << m_total;
}
public:
Bank(int total)
{
m_total = total;
}
void addCustomer(Customer* customer)
{
m_list.append(customer);
}
};
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
Customer p(4, 8);
Customer q(2, 3);
Customer r(2, 7);
Bank bank(2);
p.setObjectName("P");
q.setObjectName("Q");
r.setObjectName("R");
bank.setObjectName("Bank");
bank.addCustomer(&p);
bank.addCustomer(&q);
bank.addCustomer(&r);
p.start();
q.start();
r.start();
bank.start();
return a.exec();
}
运行结构图: