linux c/c++ 后台开发之—连接池

linux c/c++ 后台开发之—连接池

标签: c++架构分布式存储  http://blog.csdn.net/coder_yi_liu/article/details/51163991
489人阅读 评论(1) 收藏 举报
本文章已收录于:

在互联网后台开发中经常需要需要范围一些公共资源,如DB,cache, MQ,  最典型的的就是mysql, memcached, redis, 以及一些代理服务;

通常在高并发,高访问量的情况下,起停连接,是不合适的,也容易将连接占满, 尤其是工作线程多的情况,如果每个工作线程建立连接,如果服务多的情况会使得连接不够用,而且连接有时候会出现空闲的情况;连接池的作用就是保持一定数量的连接,提供给多线程服务使用,通过压力测试配置合适的连接数量,实现性能和资源的最优化。

下面是自己实现的一个通用的单例多线程安全的连接池模板,模板参数T必须是一个封装好的连接类

/***************************************************************
function: connect pool template for mysql, redis, memcached ...
author: liuyi
date: 2016.04.13
version: 1.0
***************************************************************/

#ifndef CONNECT_POOL_H
#define COMMECT_POOL_H

#include <stdlib.h>
#include <iostream>
#include <vector>
#include <pthread.h>
using namespace std;

template<class T>
class connect_pool
{
	public:
		static connect_pool<T> * get_instance()
		{
			static connect_pool<T> s_instance;
			return &s_instance;
		}

		bool init(vector<T*> connect_ptrs)
		{
			if(connect_ptrs.empty())
				return false;

			pthread_mutex_lock(m_mutex);
			for(size_t i = 0; i < connect_ptrs.size(); i++)
			{
				m_used_index_vect.push_back(0);
				m_connect_vect.push_back(connect_ptrs[i]);
			}
			pthread_mutex_unlock(m_mutex);

			return true;
		}

		int get_connect_index()
		{
			int index = -1;
			int rand_index = 0;

			pthread_mutex_lock(m_mutex);

			if(0 != m_used_index_vect.size())
			{
				rand_index = rand() % m_used_index_vect.size();
			}

			for(int j = rand_index; j < m_used_index_vect.size(); j++)
			{
				if(0 == m_used_index_vect[j])
				{
					m_used_index_vect[j] = 1;
					index = j;
					break;
				}
			}

			if(index == -1)
			{
				for(int i = 0; i < rand_index; i++)
				{
					if(0 == m_used_index_vect[i])
					{
						m_used_index_vect[i] = 1;
						index = i;
						break;
					}
				}
			}

			pthread_mutex_unlock(m_mutex);
			
			return index;
		}

		T* get_connect(int index)const
		{
			pthread_mutex_lock(m_mutex);
			if(index >= 0 && index < m_connect_vect.size())
			{
				T* p = m_connect_vect[index];
				pthread_mutex_unlock(m_mutex);
				return p;
			}

			return NULL;
		}
		
		bool return_connect_2_pool(int index)
		{
			if(index < 0)
				return false;

			pthread_mutex_lock(m_mutex);
			if(index < m_used_index_vect.size())
			{
				m_used_index_vect[index] = 0;
				pthread_mutex_unlock(m_mutex);
				return true;
			}
			pthread_mutex_unlock(m_mutex);

			return false;
		}

		void remove_connect_from_pool(int index)
		{
			pthread_mutex_lock(m_mutex);
			if(index >= 0 && index < m_used_index_vect.size())
			{
				m_used_index_vect[index] = 1;
			}
			pthread_mutex_unlock(m_mutex);
		}

		bool replace_alive_connect(T* new_connect, int index)
		{
			bool ret = false;
			pthread_mutex_lock(m_mutex);
			if(index >= 0 && index < m_used_index_vect.size())
			{
				m_used_index_vect[index] = 0;
				m_connect_vect[index] = new_connect;
				ret = true;
			}
			pthread_mutex_unlock(m_mutex);

			return ret;
		}

	private:
		connect_pool()
		{
			m_mutex = new pthread_mutex_t;
			pthread_mutex_init(m_mutex, NULL);
			srand(time(NULL));
		}

		~connect_pool()
		{
			if(NULL != m_mutex)
			{
				delete m_mutex;
				m_mutex = NULL;
			}
		}

	private:
		pthread_mutex_t *m_mutex;
		vector<int> m_used_index_vect;
		vector<T*> m_connect_vect;
};

#endif

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值