物件池的c 實現

2021-09-28 16:36:30 字數 3083 閱讀 3283

作用:

物件池是用於管理和**物件的,物件池適合緩解大的物件的頻繁申請和釋放問題。

**實現如下:

/*

#title: object pool.

@desc: 物件池,以池化的思想來減少物件的申請和釋放操作.

@author: gdl.

@version: 0.0.1

@date: 2019.10.15 22:43

@modify: $date

@reference: 《深入應用c++11》 - 祁宇 - p238.

*/#ifndef objectpool_gdl_hpp

#define objectpool_gdl_hpp

//物件池設計:

/* @需求:

1. 物件池可以接受任意建構函式.

2. 物件池大小可以動態調整.

@使用要求:

1. 物件池對外提供介面:

objectpool(size_t n = base_object_size, constructor&& constructor = ):

parameter:

n:size_t :物件池的初始大小,

constructor:constructor&& :物件池物件儲存的物件構造器,(預設為預設構造.)

inline std::shared_ptrobjectpool::get()

parameter:

返回乙個物件.

2. 物件池儲存的class必須具有clean成員函式,建議繼承class clean.

3. 使用#define auto_extend開啟物件池的自動擴漲功能,當物件不足時自動新增物件. 預設關閉此功能呢.(ps: 自動排程這塊不夠完善.)

@note:

物件池在大多數時候只適用於大物件。

&also:

還可以契合ioc模型,來擴充套件任意型別物件物件池; 但是這樣做介面會變得負責,所以先不這樣做.

*/#include#include#include#include#include#include#include#include#include#includenamespace gdl ;\

};\ has_member(clean)

/*class counter ;

void setnumber(int n)

void trigger(int c, trigger&& cb)

private:

int count;

int number;

};*/ class clean ;

templateclass objectpool ) : _cap(n), creator(constructor)

inline std::shared_ptrget()

);std::shared_ptrshptr = pool.front();

pool.pop_front();

return shptr;

} ~objectpool() ;

size_t size()

size_t capcity()

private:

templateinline typename std::enable_if::value>::type alloc(size_t n)

templateinline typename std::enable_if::value>::type alloc(size_t n)

));}

} inline void extend()

}} void deletor(t* ptr)

//t物件必須具有**自清理函式, 介面. 從而使得其在**後可以復用.

ptr->clean();

std::unique_locklock(mut);

pool.emplace_back(std::shared_ptr(ptr, [this](t* p) ));

lock.unlock();

cv.notify_one();

} private:

bool exited;

size_t _cap;

std::mutex mut;

std::condition_variable cv;

constructor creator;

std::list> pool;

};}#endif

物件池的使用示例:

#include"objectpool.hpp"

#include#include"threadpool.hpp"

#include#include"timer.hpp"

class string : public gdl::clean

void clean()

std::string data()

private:

std::string str;

};std::mutex mut;

void test_for_objectpool() );

std::cout <<"物件池大小:"<< objectpool.capcity() << std::endl;

threadpool thpool(2);

std::cout << "執行緒池池大小:" << thpool.idlcount() << std::endl;

std::cout << "init elapsed " << timer.elapsed() << " ms" << std::endl;

timer.reset();

for (int i = 0; i < 100000; i++) );

} while (thpool.idlcount() != 2)

std::cout << "take task elapsed " << timer.elapsed() << " ms" << std::endl;

}

關於標頭檔案文件:

#include"timer.hpp":

#include"threadpool.hpp":

本專案基於c++1x開發,gcc4.8以上

c 物件池的實現

ifndef cellobjectpool hpp define cellobjectpool hpp include include include ifdef debug ifndef xprintf include define xprintf printf va args endif els...

C 物件池實現

在實際中,我們會遇到乙個類最多隻允許若干個物件同時存在的情形。如果這個類的物件會被頻繁的建立,使用並銷毀,那這時會對系統效能造成影響,而這時可以考慮使用物件池的方法來避免每次使用物件都需要從 構造 使用 銷毀 這個流程,物件池中的每個物件都一次構造多次使用,而析構也只會在物件池析構是才會發生。要實現...

c 模擬實現物件池

怪物基類 pragma once 怪物列舉 enum monstertype class monster virtual monster 虛析構 monstertype gettype const private monstertype type 怪物型別 三個子類怪物 pragma once in...