#ifndef _cellobjectpool_hpp_
#define _cellobjectpool_hpp_
#include
#include
#include
#ifdef _debug
#ifndef xprintf
#include
#define xprintf(...) printf(__va_args__)
#endif
#else
#ifndef xprintf
#define xprintf(...)
#endif
#endif // _debug
template
class cellobjectpool
~cellobjectpool()
private:
class nodeheader
;public:
//釋放物件記憶體
void freeobjmemory(void* pmem)
pblock->pnext = _pheader;
_pheader = pblock;
}else
delete pblock;}}
//申請物件記憶體
void* allocobjmemory(size_t nsize)
else
xprintf("allocobjmemory: %llx, id=%d, size=%d\n", preturn, preturn->nid, nsize);
return ((char*)preturn + sizeof(nodeheader));
}private:
//初始化物件池
void initpool()
}private:
//nodeheader* _pheader;
//物件池記憶體快取區位址
char* _pbuf;
//std::mutex _mutex;
template
class objectpoolbase
void operator delete(void* p)
template
static type* createobject(args ... args)
static void destroyobject(type* obj)
private:
//typedef cellobjectpoolclasstypepool;
//static classtypepool& objectpool()
};#endif // !_cellobjectpool_hpp_
//#include"alloctor.h"
#include
#include
#include
#include//鎖
#include
#include"celltimestamp.hpp"
#include"cellobjectpoor.hpp"
using namespace std;
//原子操作 原子 分子
mutex m;
const int tcount = 8;
const int mcount = 100000;
const int ncount = mcount / tcount;
void workfun(int index)
for (size_t i = 0; i < ncount; i++)
}//搶占式
class classa : public objectpoolbase
~classa()
public:
int num = 0;
};class classb : public objectpoolbase
~classb()
public:
int num = 0;
};classa& fun(classa& pa)
void fun(shared_ptr& pa)
void fun(classa* pa)
class pp;};
int main()
celltimestamp ttime;
for (int n = 0; n < tcount; n++)
cout << ttime.getelapsedtimeinmillisec() << endl;
cout << "hello,main thread." << endl;
*//*
int* a = new int;
*a = 100;
delete a;
//printf("a=%d\n", *a);
//c++標準庫智慧型指標的一種
shared_ptrb = make_shared();
*b = 100;
//printf("b=%d\n", *b);
*//*
cout << ttime.getelapsedtimeinmillisec() << endl;
}cout << ttime.getelapsedtimeinmillisec() << endl;}*/
classa* a1 = new classa(5);
delete a1;
classa* a2 = classa::createobject(6);
classa::destroyobject(a2);
classb* b1 = new classb(5, 6);
delete b1;
classb* b2 = classb::createobject(5, 6);
classb::destroyobject(b2);
return 0;
}
物件池的c 實現
作用 物件池是用於管理和 物件的,物件池適合緩解大的物件的頻繁申請和釋放問題。實現如下 title object pool.desc 物件池,以池化的思想來減少物件的申請和釋放操作.author gdl.version 0.0.1 date 2019.10.15 22 43 modify date ...
C 物件池實現
在實際中,我們會遇到乙個類最多隻允許若干個物件同時存在的情形。如果這個類的物件會被頻繁的建立,使用並銷毀,那這時會對系統效能造成影響,而這時可以考慮使用物件池的方法來避免每次使用物件都需要從 構造 使用 銷毀 這個流程,物件池中的每個物件都一次構造多次使用,而析構也只會在物件池析構是才會發生。要實現...
c 模擬實現物件池
怪物基類 pragma once 怪物列舉 enum monstertype class monster virtual monster 虛析構 monstertype gettype const private monstertype type 怪物型別 三個子類怪物 pragma once in...