石门-archy 2016-04-20 2157浏览量
这次使用的内存池是原公司的,利用hash_map做的,大概的接口是这样:
template <class CObject>
class ObjectPool
{ <br>public:<br> typedef hash_map<int64_t,ObjectBlock*> BlockMap;
virtual int pop(CObject *&ob);
virtual int push(const CObject *ob);<br>private:<br> BlockMap _block_map;
};
#define MAX_SIZE 50000
struct MsgToPut
{
UINT _wr_ptr;
UINT _rd_ptr;
UINT _length;
CHAR _base[1024];
void reset()
{
this->_rd_ptr = this->_wr_ptr = 0;
this->_length = 1024;
}
};
ObjectPool<MsgToPut> MsgPool;
void* newthread(void* argv)
{
long long int start = get_os_system_time();
for(int i=0;i<MAX_SIZE;i++)
{
MsgToPut* msg;
MsgPool.pop(msg);
if(msg == NULL)
{
printf("error\n");
}
}
long long int end = get_os_system_time();
printf("newthread ObjectPool:%d\n",end - start);
MsgToPut* mtp[MAX_SIZE];
for(int i=0;i<MAX_SIZE;i++)
{
mtp[i] = (MsgToPut*)tc_malloc(sizeof(MsgToPut));
mtp[i]->reset();
}
start = get_os_system_time();
printf("newthread tc malloc :%d\n",start - end);
return (void*)0;
}
int main()
{
pthread_t pid;
pthread_create(&pid,NULL,newthread,NULL);
pthread_create(&pid,NULL,newthread,NULL);
pthread_create(&pid,NULL,newthread,NULL);
long long int start = get_os_system_time();
for(int i=0;i<MAX_SIZE;i++)
{
MsgToPut* msg = NULL;
MsgPool.pop(msg);
if(msg == NULL)
{
printf("error\n");
}
}
long long int end = get_os_system_time();
printf("main ObjectPool:%d\n",end - start);
MsgToPut* mtp[MAX_SIZE];
for(int i=0;i<MAX_SIZE;i++)
{
mtp[i] = (MsgToPut*)tc_malloc(sizeof(MsgToPut));
mtp[i]->reset();
}
start = get_os_system_time();
printf("main tc malloc :%d\n",start - end);
return 0;
}
普遍打印数据为:
newthread ObjectPool:189
newthread ObjectPool:202
newthread tc malloc :24
newthread tc malloc :20
main ObjectPool:235
newthread ObjectPool:235
main tc malloc :21
newthread tc malloc :22
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。
时时分享云计算技术内容,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。