MySQL数据库编程、单例模式、queue队列容器、C++11多线程编程、线程互斥、线程同步通信和 unique_lock、基于CAS的原子整形、智能指针shared_ptr、lambda表达式、生产者-消费者线程模型
为了提高MySQL数据库(基于C/S设计)的访问瓶颈,除了在服务器端增加缓存服务器缓存常用的数据之外(例如redis),还可以增加连接池来提高MySQL Server的访问效率,在高并发情况下,
大量的TCP三次握手、MySQL Server连接认证、MySQL Server关闭连接回收资源和TCP四次挥手所耗费的性能时间也是很明显的,增加连接池就是为了减少这一部分的性能损耗。
在市场上比较流行的连接池包括阿里的druid,c3p0以及apache dbcp连接池,它们对于短时间内大量的数据库增删改查操作性能的提升是很明显的,但是它们有一个共同点就是,全部由Java实现的。
那么本项目就是为了在C/C++项目中,提供MySQL Server的访问效率,实现基于C++代码的数据库连接池模块。
有关MySQL数据库编程、多线程编程、线程互斥和同步通信操作、智能指针、设计模式、容器等等这些技术在C++语言层面都可以直接实现。本项目选择在Linux下进行开发,Linux版本是:
Linux version 5.15.0-67-generic (buildd@lcy02-amd64-029) (gcc (Ubuntu 9.4.0-1ubuntu1~20.04.1) 9.4.0, GNU ld (GNU Binutils for Ubuntu) 2.34)
show variables like 'max_connections';
该命令可以查看MySQL Server所支持的最大连接个数,超过max_connections数量的连接,MySQL Server会直接拒绝,所以在使用连接池增加连接数量的时候,MySQL Server的max_connections参数也要适当的进行调整,以适配连接池的连接上限。
CREATE DATABASE chat;
CREATE TABLE IF NOT EXISTS `user`(
`id` INT(11) AUTO_INCREMENT,
`name` VARCHAR(150) DEFAULT NULL,
`age` int(11) DEFAULT NULL,
`sex` enum('male','female') DEFAULT NULL,
PRIMARY KEY ( `id` )
)ENGINE=InnoDB DEFAULT CHARSET=utf8;
该类主要实现数据库操作
// Connection.h
#ifndef CONNECTION_H
#define CONNECTION_H
// 实现数据库的操作
#include
#include
using namespace std;
class Connection
{
public:
// 初始化数据库连接
Connection();
~Connection();
bool connect(string ip, unsigned short port, string username, string password, string dbname);
// 更新操作, insert delete update
bool update(string sql);
// 查询 select
MYSQL_RES *query(string sql);
// 刷新连接的起始空闲时间
void refreshAliveTime() { _alivetime = clock();}
// 返回空闲的时间
clock_t getAliveTime() const {return clock() - _alivetime; }
private:
MYSQL *_conn; // 和MYSQL Server建立的一条连接
clock_t _alivetime; // 进入空闲状态的起始时间
};
#endif
// Connection.cpp
#include "Connection.h"
#include "public.h"
#include
using namespace std;
Connection::Connection()
{
// 内部猜测分配内存
_conn = mysql_init(nullptr);
}
Connection::~Connection()
{
if(_conn != nullptr)
mysql_close(_conn);
}
bool Connection::connect(string ip, unsigned short port, string username, string password, string dbname)
{
MYSQL *p = mysql_real_connect(_conn, ip.c_str(), username.c_str(), password.c_str(),
dbname.c_str(), port, nullptr, 0);
return p != nullptr;
}
// 更新操作, insert delete update
bool Connection::update(string sql)
{
// mysql_query返回0表示成功
if(mysql_query(_conn, sql.c_str()))
{
LOG("更新失败 : " + sql + "\n你不是合格的CRUD崽");
cout << mysql_error(_conn) << endl;
return false;
}
// LOG("更新成功 : " + sql + "\n 你是一个合格的CRUD崽");
return true;
}
// 查询 select
MYSQL_RES* Connection::query(string sql)
{
if(mysql_query(_conn, sql.c_str()));
{
LOG("查询失败 : " + sql + "不是合格的CRUD崽");
return nullptr;
}
return mysql_use_result(_conn);
}
// CommonConnectionPool.h
// 防止头文件重复包含
#ifndef CONNECTIONPOOL_H
#define CONNECTIONPOOL_H
// 连接池功能模块
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include "Connection.h"
using namespace std;
class ConnectionPool
{
public:
// 获取连接池实例对象
static ConnectionPool* getConnectionPool();
// 给外部提供接口,从连接池中,获取一个可用连接,消费行为
// 使用智能指针,在使用连接后自动归还连接
shared_ptr<Connection> getConnection();
private:
ConnectionPool(); // 单例模式,构造函数私有化
// 从配置文件中解析配置项
bool loadConfigFile();
// 生产新的连接,运行在独立的线程中
void produceConnectionTask();
// 扫描空闲超过maxIdleTime的连接,运行在独立的线程中
void scannerConnectionTask();
// 数据库相关参数
string _ip;
unsigned short _port;
string _username;
string _password;
string _dbname;
int _initSize; // 连接池的初始连接量
int _maxSize; // 连接池的最大连接量
int _maxIdleTime; // 连接的最大空闲时间(s),实现定时关闭多余连接,保持_initSize数量的连接
int _connectionTimeout; // 连接池获取连接的超时时间(ms)
queue<Connection*> _connectionQue; // 存储mysql连接的队列
mutex _queueMutex; // 维护连接池队列的线程安全的互斥锁
atomic_int _connectionCnt; // 记录当前池中的连接数量(是指所有的连接, 而不是当前队列中的连接数量)
condition_variable cv; // 条件变量,用于生产者消费者之间的线程同步(通信)
};
#endif
实现
// CommonConnectionPool.cpp
#include "CommonConnectionPool.h"
#include "public.h"
// static函数定义,不需要带static
ConnectionPool *ConnectionPool::getConnectionPool()
{
// C++11保证线程安全
static ConnectionPool pool; // 编译器内部进行lock和unlock
return &pool;
}
bool ConnectionPool::loadConfigFile()
{
FILE *pf = fopen("mysql.conf", "r");
if (pf == nullptr)
{
LOG("mysql.conf file is not exist");
return false;
}
// feof判断文件是否到结尾
while (!feof(pf))
{
char line[1024] = {0};
fgets(line, 1024, pf); // 读取一行
string str = line;
int idx = str.find('=', 0);
if (idx == -1) // 无效配置项
{
continue;
}
// username=root
// parse
int endidx = str.find('\n', idx);
string key = str.substr(0, idx);
string value = str.substr(idx + 1, endidx - idx - 1);
// cout << "key:" << key << ", value :" << value << endl;
if (key == "ip")
{
_ip = value;
}
else if (key == "port")
{
_port = atoi(value.c_str());
}
else if (key == "username")
{
_username = value;
}
else if (key == "password")
{
_password = value;
}
else if (key == "dbname")
{
_dbname = value;
}
else if (key == "initSize")
{
_initSize = atoi(value.c_str());
}
else if (key == "maxSize")
{
_maxSize = atoi(value.c_str());
}
else if (key == "maxIdleTime")
{
_maxIdleTime = atoi(value.c_str());
}
else if (key == "connectionTimeOut")
{
_connectionTimeout = atoi(value.c_str());
}
}
return true;
}
ConnectionPool::ConnectionPool()
{
// 加载配置项
if (!loadConfigFile())
{
return;
}
// 创建初始数量的线程
for (int i = 0; i < _initSize; ++i)
{
Connection *p = new Connection();
p->connect(_ip, _port, _username, _password, _dbname);
p->refreshAliveTime(); // 刷新空闲开始的时间
_connectionQue.push(p);
_connectionCnt++;
}
// 启动一个新的线程的,执行生产者任务 linux thread => pthread_create
// !!传递成员函数做线程执行函数时需绑定对象
thread produce(std::bind(&ConnectionPool::produceConnectionTask, this)); // b
// detach()函数,让线程在后台运行住,意味着线程不能与之产生直接交互,后台线程的归属和控制都由C++运行时库处理。
produce.detach();
// 启动一个新的线程,执行定时扫描超过maxIdleTime空闲时间的连接,进行对于连接的回收
thread scanner(std::bind(&ConnectionPool::scannerConnectionTask, this));
// detach()函数,让线程在后台运行住,意味着线程不能与之产生直接交互,后台线程的归属和控制都由C++运行时库处理。
scanner.detach();
}
void ConnectionPool::produceConnectionTask()
{
for (;;)
{
unique_lock<mutex> lock(_queueMutex);
while (!_connectionQue.empty())
{
// 有产品就不生产,内部会解锁
cv.wait(lock); // 队列不空,生产者线程进入等待
}
if (_connectionCnt < _maxSize)
{
Connection *p = new Connection();
p->connect(_ip, _port, _username, _password, _dbname);
p->refreshAliveTime(); // 刷新空闲开始的时间
_connectionQue.push(p);
_connectionCnt++;
}
// 通知消费者线程,可以消费连接了
cv.notify_all();
}
}
shared_ptr<Connection> ConnectionPool::getConnection()
{
unique_lock<mutex> lock(_queueMutex);
// 注意这里用while,是保证wait_for被唤醒后,确实是从队列中取走连接了
// 有可能notify唤醒后该线程其实并没有获取到连接(waitfor内部锁加慢了一步,被别的消费者抢先了)
while(_connectionQue.empty())
{
if(cv_status::timeout == cv.wait_for(lock, chrono::milliseconds(_connectionTimeout)));
{
if(_connectionQue.empty())
{
LOG("获取空闲连接超时...获取连接失败!");
return nullptr;
}
}
}
// shared_ptr 析构时,会把connection直接delete掉,即调用connection析构
// connection就被close了
// 所以这里自定义shared_ptr释放资源的方式,把connection直接归还到queue中
shared_ptr<Connection> sp(_connectionQue.front(),
[&](Connection *pcon){
// 这里是在服务器应用线程中调用的,所以也要考虑队列线程安全
unique_lock<mutex> lock(_queueMutex);
pcon->refreshAliveTime(); // 刷新空闲开始的时间
_connectionQue.push(pcon);
});
_connectionQue.pop();
cv.notify_all(); // 消费后通知生产者检查队列是否为空,为空则生产连接否则不做任何事
return sp;
}
void ConnectionPool::scannerConnectionTask()
{
for(;;)
{
// sleep模拟定时效果,周期轮询
this_thread::sleep_for(chrono::seconds(_maxIdleTime));
// ???这个锁能往循环里放减小粒度吗
unique_lock<mutex> lock(_queueMutex);
// 只有连接总数大于初始连接数才考虑释放连接
while(_connectionCnt > _initSize)
{
// unique_lock lock(_queueMutex);
Connection *p = _connectionQue.front();
if(p->getAliveTime() >= (_maxIdleTime * 1000))
{
_connectionQue.pop();
_connectionCnt--;
delete p;
}
else
{
// 队头连接(最早变为空闲状态)都没有空闲超过_maxIdleTime ,后续连接更不会超过
break;
}
}
}
}
# mysql.conf
# 数据库连接池配置文件
ip=127.0.0.1
port=3306
username=root
password=123456
dbname=chat
initSize=10
maxSize=1024
#最大空闲时间默认单位是秒
maxIdleTime=60
#连接超时时间为ms
connectionTimeOut=100
#pragma once
#define LOG(str) \
cout << __FILE__ << ":" << __LINE__ << " " << \
__TIMESTAMP__ << " : " << str << endl;
makefile文件如下
CXX ?= g++ #如果没有被赋值过就赋予等号后面的值
DEBUG ?= 1
ifeq ($(DEBUG), 1)
CXXFLAGS += -g
else
CXXFLAGS += -O2
endif
main: main.cpp Connection.cpp CommonConnectionPool.cpp
$(CXX) -o main $^ $(CXXFLAGS) -pthread -lmysqlclient
clean:
rm -r main
测试代码
// main.cpp
#include
#include
#include "Connection.h"
#include "CommonConnectionPool.h"
using namespace std;
// 一个MYSQL请求就建立一条连接,请求结束后就释放连接
void test_withoutPool()
{
#if 0 // 单线程1000个用户都做一次任务
for(int i = 0; i < 5000; ++i)
{
Connection conn;
char sql[1024] = {0};
sprintf(sql, "insert into user(name,age,sex) values('%s', %d, '%s')",
"zhangke", 25, "male");
// 连接到数据库
conn.connect("127.0.0.1", 3306, "root", "123456", "chat");
conn.update(sql);
}
#else // 多线程,4线程
thread t1([](){
for(int i = 0; i < 1250; ++i)
{
Connection conn;
char sql[1024] = {0};
sprintf(sql, "insert into user(name,age,sex) values('%s', %d, '%s')",
"zhangke", 25, "male");
// 连接到数据库
conn.connect("127.0.0.1", 3306, "root", "123456", "chat");
conn.update(sql);
}
});
thread t2([](){
for(int i = 0; i < 1250; ++i)
{
Connection conn;
char sql[1024] = {0};
sprintf(sql, "insert into user(name,age,sex) values('%s', %d, '%s')",
"zhangke", 25, "male");
// 连接到数据库
conn.connect("127.0.0.1", 3306, "root", "123456", "chat");
conn.update(sql);
}
});
thread t3([](){
for(int i = 0; i < 1250; ++i)
{
Connection conn;
char sql[1024] = {0};
sprintf(sql, "insert into user(name,age,sex) values('%s', %d, '%s')",
"zhangke", 25, "male");
// 连接到数据库
conn.connect("127.0.0.1", 3306, "root", "123456", "chat");
conn.update(sql);
}
});
thread t4([](){
for(int i = 0; i < 1250; ++i)
{
Connection conn;
char sql[1024] = {0};
sprintf(sql, "insert into user(name,age,sex) values('%s', %d, '%s')",
"zhangke", 25, "male");
// 连接到数据库
conn.connect("127.0.0.1", 3306, "root", "123456", "chat");
conn.update(sql);
}
});
t1.join();
t2.join();
t3.join();
t4.join();
#endif
}
void test_withPool()
{
#if 0 // 单线程
ConnectionPool *pool = ConnectionPool::getConnectionPool();
// 1000个用户都做一次任务
for (int i = 0; i < 5000; ++i)
{
shared_ptr<Connection> sp = pool->getConnection();
char sql[1024] = {0};
sprintf(sql, "insert into user(name,age,sex) values('%s', %d, '%s')",
"zhangke", 25, "male");
sp->update(sql);
}
#else // 多线程 4
thread t1([](){
ConnectionPool *pool = ConnectionPool::getConnectionPool();
// 1000个用户都做一次任务
for (int i = 0; i < 1250; ++i)
{
shared_ptr<Connection> sp = pool->getConnection();
char sql[1024] = {0};
sprintf(sql, "insert into user(name,age,sex) values('%s', %d, '%s')",
"zhangke", 25, "male");
sp->update(sql);
}
});
thread t2([](){
ConnectionPool *pool = ConnectionPool::getConnectionPool();
// 1000个用户都做一次任务
for (int i = 0; i < 1250; ++i)
{
shared_ptr<Connection> sp = pool->getConnection();
char sql[1024] = {0};
sprintf(sql, "insert into user(name,age,sex) values('%s', %d, '%s')",
"zhangke", 25, "male");
sp->update(sql);
}
});
thread t3([](){
ConnectionPool *pool = ConnectionPool::getConnectionPool();
// 1000个用户都做一次任务
for (int i = 0; i < 1250; ++i)
{
shared_ptr<Connection> sp = pool->getConnection();
char sql[1024] = {0};
sprintf(sql, "insert into user(name,age,sex) values('%s', %d, '%s')",
"zhangke", 25, "male");
sp->update(sql);
}
});
thread t4([](){
ConnectionPool *pool = ConnectionPool::getConnectionPool();
// 1000个用户都做一次任务
for (int i = 0; i < 1250; ++i)
{
shared_ptr<Connection> sp = pool->getConnection();
char sql[1024] = {0};
sprintf(sql, "insert into user(name,age,sex) values('%s', %d, '%s')",
"zhangke", 25, "male");
sp->update(sql);
}
});
t1.join();
t2.join();
t3.join();
t4.join();
#endif
}
int main()
{
struct timeval start, end;
gettimeofday(&start, NULL);
// test_withoutPool();
test_withPool();
gettimeofday(&end, NULL);
long long total_time = (end.tv_sec - start.tv_sec) * 1000000 + (end.tv_usec - start.tv_usec);
cout << total_time / 1000 << "ms" << endl;
return 0;
}
数据量 | 未使用连接池 | 使用连接池 |
---|---|---|
1000 | 单线程:216725ms 四线程:53839ms | 单线程:2722ms 四线程:2476ms |
2000 | 单线程:431956ms 四线程:107929ms | 单线程: 3108ms 四线程:2695ms |
5000 | 单线程:1082188ms 四线程:272680ms | 单线程:4755ms 四线程:3396ms |
可见采用连接池后性能得到大幅度的提升,同时使用多线程也可以提升并发连接的效率
unique_lock 和 lock_guard 都是C++11中的互斥锁RAII(Resource Acquisition Is Initialization)类,用于管理互斥锁的生命周期,从而保证代码的线程安全性。
它们的区别在于:
灵活性不同
unique_lock 比 lock_guard 更加灵活,因为它提供了更多的操作。unique_lock 对象可以随时unlock()和lock()多次,而 lock_guard 对象一旦被初始化就不可以再unlock()或lock()了。
锁定时间不同
unique_lock 的锁定时间比 lock_guard 长,因为它可以延迟锁定(deferred lock),即可以在不锁定互斥量的情况下创建一个 unique_lock 对象,然后在合适的时候再锁定互斥量。这个功能对于需要跨多个函数的操作非常有用。
开销不同
由于 unique_lock 更加灵活,所以在使用时会产生更多的开销。相比之下,lock_guard 对象的构造和析构函数所需的开销较小。
所有权转移
unique_lock 对象可以转移所有权(move ownership),这意味着一个 unique_lock 对象可以被移动到另一个对象中,而不需要释放锁,这对于编写返回锁定的函数非常有用。
总的来说,如果你需要更加灵活的互斥量管理方式并且可以容忍稍微的开销,那么 unique_lock 是一个不错的选择。如果你只需要简单的锁定,并且需要尽可能地减小开销,那么 lock_guard 是更好的选择。
线程安全的单例模式