-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathpgPool.cpp
46 lines (35 loc) · 909 Bytes
/
pgPool.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include "pgPool.h"
PGPool::PGPool()
{
createPool();
}
void PGPool::createPool()
{
std::lock_guard<std::mutex> locker_( m_mutex );
for (auto i=0; i < POOL_SIZE; i++) {
m_pool.emplace(std::make_shared<pqxx::lazyconnection>());
}
}
std::shared_ptr<pqxx::lazyconnection> PGPool::connection()
{
std::unique_lock<std::mutex> lock_(m_mutex);
// if pool is empty, then wait until it notifies back
while (m_pool.empty()) {
m_condition.wait(lock_);
}
// get new connection in queue
auto conn_ = m_pool.front();
// immediately pop as we will use it now
m_pool.pop();
return conn_;
}
void PGPool::freeConnection(std::shared_ptr<pqxx::lazyconnection> conn_)
{
std::unique_lock<std::mutex> lock_(m_mutex);
// push a new connection into a pool
m_pool.push(conn_);
// unlock mutex
lock_.unlock();
// notify one of thread that is waiting
m_condition.notify_one();
}