Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CommonConnectionPool.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ class Connectionpool
shared_ptr<Connection> getConnection();//���ⲿ�ṩ�ӿڣ������ӳػ�ȡһ�����õĿ�������
private:
Connectionpool();//����1
~Connectionpool();

bool loadConfigFile();//�������ļ��м���������
//�����ڶ������߳��У�ר�Ÿ������������
Expand All @@ -38,4 +39,7 @@ class Connectionpool
mutex _queueMutex;//ά�����Ӷ��е��̰߳�ȫ������
atomic_int _connectionCnt;//��¼��������connection���ӵ�������
condition_variable cv;//

std::atomic_bool _stop;
std::counting_semaphore<1> _sem{0};
};
24 changes: 20 additions & 4 deletions CommonConnectionpool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ bool Connectionpool::loadConfigFile()
return true;
}

Connectionpool::Connectionpool()
Connectionpool::Connectionpool:_stop(false)()
{

if (!loadConfigFile())
Expand Down Expand Up @@ -103,11 +103,11 @@ void Connectionpool::produceConnectionTask()
for (;;)
{
unique_lock<mutex> lock(_queueMutex);
while (!_connectionQue.empty())
while (!_connectionQue.empty()&&!_stop)
{
cv.wait(lock); //
}

if(_stop){break;}

if (_connectionCnt < _maxSize)
{
Expand Down Expand Up @@ -176,5 +176,21 @@ void Connectionpool::scannerConnectionTask()
break; // ��ͷ������û�г���_maxIdleTime���������ӿ϶�û��
}
}

if(_stop) {
while(!_connectionQue.empty()) {
Connection *p = _connectionQue.front();
_connectionQue.pop();
delete p;
}
_sem.release();
break;
}
}
}
}

ConnectionPool::~ConnectionPool() {
_stop = true;
cv.notify_all(); // ģ��һ�������ߣ�����������
_sem.acquire();
}