C++ 对象池深度解析:架构设计与实现原理

🎯 C++ 对象池深度解析:架构设计与实现原理


🔗 项目地址

  • 项目仓库 : cpp-misc
  • 代码位置 : include/objPool/
  • 模块文件 :
    • objPool.hpp - 对象池主类
    • allocator.hpp - 对象分配器
    • memPool.hpp - 内存池实现

一、对象池模式概述

1.1 什么是对象池

对象池是一种设计模式,其核心思想是预先分配一组对象实例,在需要时从池中获取,使用完毕后归还给池,而不是频繁地创建和销毁对象。

1.2 为什么需要对象池

在高性能应用中,频繁创建和销毁对象会带来显著的性能开销:

开销类型 具体表现 影响
内存分配 频繁调用 new/delete 内存碎片、分配器锁竞争
构造/析构 对象初始化和清理 CPU 时间消耗
缓存失效 对象在内存中分散 CPU 缓存命中率降低

1.3 对象池的适用场景

#mermaid-svg-feGCVaVEDVFcRx3u{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-feGCVaVEDVFcRx3u .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-feGCVaVEDVFcRx3u .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-feGCVaVEDVFcRx3u .error-icon{fill:#552222;}#mermaid-svg-feGCVaVEDVFcRx3u .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-feGCVaVEDVFcRx3u .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-feGCVaVEDVFcRx3u .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-feGCVaVEDVFcRx3u .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-feGCVaVEDVFcRx3u .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-feGCVaVEDVFcRx3u .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-feGCVaVEDVFcRx3u .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-feGCVaVEDVFcRx3u .marker{fill:#333333;stroke:#333333;}#mermaid-svg-feGCVaVEDVFcRx3u .marker.cross{stroke:#333333;}#mermaid-svg-feGCVaVEDVFcRx3u svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-feGCVaVEDVFcRx3u p{margin:0;}#mermaid-svg-feGCVaVEDVFcRx3u .pieCircle{stroke:#000000;stroke-width:2px;opacity:0.7;}#mermaid-svg-feGCVaVEDVFcRx3u .pieOuterCircle{stroke:#000000;stroke-width:1px;fill:none;}#mermaid-svg-feGCVaVEDVFcRx3u .pieTitleText{text-anchor:middle;font-size:25px;fill:#000000;font-family:"trebuchet ms",verdana,arial,sans-serif;}#mermaid-svg-feGCVaVEDVFcRx3u .slice{font-family:"trebuchet ms",verdana,arial,sans-serif;fill:#000000;font-size:17px;}#mermaid-svg-feGCVaVEDVFcRx3u .legend text{fill:#000000;font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:17px;}#mermaid-svg-feGCVaVEDVFcRx3u :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;} 35% 25% 20% 15% 5% 对象池适用场景分布 网络连接管理 游戏实体管理 数据库连接池 线程池任务对象 其他


二、wheels::objPool 架构设计

2.1 整体架构层次

#mermaid-svg-sTWNN1bbyrZxpe5g{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-sTWNN1bbyrZxpe5g .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-sTWNN1bbyrZxpe5g .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-sTWNN1bbyrZxpe5g .error-icon{fill:#552222;}#mermaid-svg-sTWNN1bbyrZxpe5g .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-sTWNN1bbyrZxpe5g .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-sTWNN1bbyrZxpe5g .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-sTWNN1bbyrZxpe5g .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-sTWNN1bbyrZxpe5g .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-sTWNN1bbyrZxpe5g .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-sTWNN1bbyrZxpe5g .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-sTWNN1bbyrZxpe5g .marker{fill:#333333;stroke:#333333;}#mermaid-svg-sTWNN1bbyrZxpe5g .marker.cross{stroke:#333333;}#mermaid-svg-sTWNN1bbyrZxpe5g svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-sTWNN1bbyrZxpe5g p{margin:0;}#mermaid-svg-sTWNN1bbyrZxpe5g .label{font-family:"trebuchet ms",verdana,arial,sans-serif;color:#333;}#mermaid-svg-sTWNN1bbyrZxpe5g .cluster-label text{fill:#333;}#mermaid-svg-sTWNN1bbyrZxpe5g .cluster-label span{color:#333;}#mermaid-svg-sTWNN1bbyrZxpe5g .cluster-label span p{background-color:transparent;}#mermaid-svg-sTWNN1bbyrZxpe5g .label text,#mermaid-svg-sTWNN1bbyrZxpe5g span{fill:#333;color:#333;}#mermaid-svg-sTWNN1bbyrZxpe5g .node rect,#mermaid-svg-sTWNN1bbyrZxpe5g .node circle,#mermaid-svg-sTWNN1bbyrZxpe5g .node ellipse,#mermaid-svg-sTWNN1bbyrZxpe5g .node polygon,#mermaid-svg-sTWNN1bbyrZxpe5g .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-sTWNN1bbyrZxpe5g .rough-node .label text,#mermaid-svg-sTWNN1bbyrZxpe5g .node .label text,#mermaid-svg-sTWNN1bbyrZxpe5g .image-shape .label,#mermaid-svg-sTWNN1bbyrZxpe5g .icon-shape .label{text-anchor:middle;}#mermaid-svg-sTWNN1bbyrZxpe5g .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-sTWNN1bbyrZxpe5g .rough-node .label,#mermaid-svg-sTWNN1bbyrZxpe5g .node .label,#mermaid-svg-sTWNN1bbyrZxpe5g .image-shape .label,#mermaid-svg-sTWNN1bbyrZxpe5g .icon-shape .label{text-align:center;}#mermaid-svg-sTWNN1bbyrZxpe5g .node.clickable{cursor:pointer;}#mermaid-svg-sTWNN1bbyrZxpe5g .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-sTWNN1bbyrZxpe5g .arrowheadPath{fill:#333333;}#mermaid-svg-sTWNN1bbyrZxpe5g .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-sTWNN1bbyrZxpe5g .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-sTWNN1bbyrZxpe5g .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-sTWNN1bbyrZxpe5g .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-sTWNN1bbyrZxpe5g .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-sTWNN1bbyrZxpe5g .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-sTWNN1bbyrZxpe5g .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-sTWNN1bbyrZxpe5g .cluster text{fill:#333;}#mermaid-svg-sTWNN1bbyrZxpe5g .cluster span{color:#333;}#mermaid-svg-sTWNN1bbyrZxpe5g div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-sTWNN1bbyrZxpe5g .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-sTWNN1bbyrZxpe5g rect.text{fill:none;stroke-width:0;}#mermaid-svg-sTWNN1bbyrZxpe5g .icon-shape,#mermaid-svg-sTWNN1bbyrZxpe5g .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-sTWNN1bbyrZxpe5g .icon-shape p,#mermaid-svg-sTWNN1bbyrZxpe5g .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-sTWNN1bbyrZxpe5g .icon-shape .label rect,#mermaid-svg-sTWNN1bbyrZxpe5g .image-shape .label rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-sTWNN1bbyrZxpe5g .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-sTWNN1bbyrZxpe5g .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-sTWNN1bbyrZxpe5g :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;} 系统层
内存池层
对象池层
应用层
应用程序
objPool 对象池
objPoolAllocator 分配器
memPool 内存池
pond 内存块管理
系统内存

2.2 核心组件关系图

#mermaid-svg-6tn05f4jpWZqwvej{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-6tn05f4jpWZqwvej .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-6tn05f4jpWZqwvej .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-6tn05f4jpWZqwvej .error-icon{fill:#552222;}#mermaid-svg-6tn05f4jpWZqwvej .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-6tn05f4jpWZqwvej .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-6tn05f4jpWZqwvej .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-6tn05f4jpWZqwvej .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-6tn05f4jpWZqwvej .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-6tn05f4jpWZqwvej .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-6tn05f4jpWZqwvej .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-6tn05f4jpWZqwvej .marker{fill:#333333;stroke:#333333;}#mermaid-svg-6tn05f4jpWZqwvej .marker.cross{stroke:#333333;}#mermaid-svg-6tn05f4jpWZqwvej svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-6tn05f4jpWZqwvej p{margin:0;}#mermaid-svg-6tn05f4jpWZqwvej g.classGroup text{fill:#9370DB;stroke:none;font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:10px;}#mermaid-svg-6tn05f4jpWZqwvej g.classGroup text .title{font-weight:bolder;}#mermaid-svg-6tn05f4jpWZqwvej .cluster-label text{fill:#333;}#mermaid-svg-6tn05f4jpWZqwvej .cluster-label span{color:#333;}#mermaid-svg-6tn05f4jpWZqwvej .cluster-label span p{background-color:transparent;}#mermaid-svg-6tn05f4jpWZqwvej .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-6tn05f4jpWZqwvej .cluster text{fill:#333;}#mermaid-svg-6tn05f4jpWZqwvej .cluster span{color:#333;}#mermaid-svg-6tn05f4jpWZqwvej .nodeLabel,#mermaid-svg-6tn05f4jpWZqwvej .edgeLabel{color:#131300;}#mermaid-svg-6tn05f4jpWZqwvej .edgeLabel .label rect{fill:#ECECFF;}#mermaid-svg-6tn05f4jpWZqwvej .label text{fill:#131300;}#mermaid-svg-6tn05f4jpWZqwvej .labelBkg{background:#ECECFF;}#mermaid-svg-6tn05f4jpWZqwvej .edgeLabel .label span{background:#ECECFF;}#mermaid-svg-6tn05f4jpWZqwvej .classTitle{font-weight:bolder;}#mermaid-svg-6tn05f4jpWZqwvej .node rect,#mermaid-svg-6tn05f4jpWZqwvej .node circle,#mermaid-svg-6tn05f4jpWZqwvej .node ellipse,#mermaid-svg-6tn05f4jpWZqwvej .node polygon,#mermaid-svg-6tn05f4jpWZqwvej .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-6tn05f4jpWZqwvej .divider{stroke:#9370DB;stroke-width:1;}#mermaid-svg-6tn05f4jpWZqwvej g.clickable{cursor:pointer;}#mermaid-svg-6tn05f4jpWZqwvej g.classGroup rect{fill:#ECECFF;stroke:#9370DB;}#mermaid-svg-6tn05f4jpWZqwvej g.classGroup line{stroke:#9370DB;stroke-width:1;}#mermaid-svg-6tn05f4jpWZqwvej .classLabel .box{stroke:none;stroke-width:0;fill:#ECECFF;opacity:0.5;}#mermaid-svg-6tn05f4jpWZqwvej .classLabel .label{fill:#9370DB;font-size:10px;}#mermaid-svg-6tn05f4jpWZqwvej .relation{stroke:#333333;stroke-width:1;fill:none;}#mermaid-svg-6tn05f4jpWZqwvej .dashed-line{stroke-dasharray:3;}#mermaid-svg-6tn05f4jpWZqwvej .dotted-line{stroke-dasharray:1 2;}#mermaid-svg-6tn05f4jpWZqwvej #compositionStart,#mermaid-svg-6tn05f4jpWZqwvej .composition{fill:#333333!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-6tn05f4jpWZqwvej #compositionEnd,#mermaid-svg-6tn05f4jpWZqwvej .composition{fill:#333333!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-6tn05f4jpWZqwvej #dependencyStart,#mermaid-svg-6tn05f4jpWZqwvej .dependency{fill:#333333!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-6tn05f4jpWZqwvej #dependencyStart,#mermaid-svg-6tn05f4jpWZqwvej .dependency{fill:#333333!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-6tn05f4jpWZqwvej #extensionStart,#mermaid-svg-6tn05f4jpWZqwvej .extension{fill:transparent!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-6tn05f4jpWZqwvej #extensionEnd,#mermaid-svg-6tn05f4jpWZqwvej .extension{fill:transparent!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-6tn05f4jpWZqwvej #aggregationStart,#mermaid-svg-6tn05f4jpWZqwvej .aggregation{fill:transparent!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-6tn05f4jpWZqwvej #aggregationEnd,#mermaid-svg-6tn05f4jpWZqwvej .aggregation{fill:transparent!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-6tn05f4jpWZqwvej #lollipopStart,#mermaid-svg-6tn05f4jpWZqwvej .lollipop{fill:#ECECFF!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-6tn05f4jpWZqwvej #lollipopEnd,#mermaid-svg-6tn05f4jpWZqwvej .lollipop{fill:#ECECFF!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-6tn05f4jpWZqwvej .edgeTerminals{font-size:11px;line-height:initial;}#mermaid-svg-6tn05f4jpWZqwvej .classTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-6tn05f4jpWZqwvej .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-6tn05f4jpWZqwvej .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-6tn05f4jpWZqwvej :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;} uses
uses
manages
objPool

  • pt_pool__: shared_ptr<memPool>
  • m_allocator__: objPoolAllocator
  • m_unused_list__: list<T*>
  • m_max_count__: size_t
  • m_lock__: atomic_flag
  • get() : : T
  • release(T*) : : void
  • incCountTo(size_t) : : bool
  • decCountTo(size_t) : : bool
  • clear() : : void
    objPoolAllocator
  • pt_pool__: shared_ptr<memPool>
  • allocate(size_t) : : void
  • deallocate(void*) : : void
  • setMemPool(shared_ptr<memPool>) : : void
    memPool
  • m_ponds__: unordered_map<size_t, pond*>
  • m_lock__: atomic_flag
  • registerType(size_t, size_t) : : void
  • malloc(size_t) : : void
  • free(size_t, void*) : : bool
    pond
  • m_unused__: unordered_set<drop_t>
  • m_used__: unordered_set<drop_t>
  • m_count__: size_t
  • m_size__: size_t
  • malloc() : : drop_t
  • free(drop_t) : : void
  • reCount(size_t) : : bool

2.3 架构设计亮点

设计特点 实现方式 优势
分层架构 对象池 → 分配器 → 内存池 → 内存块 职责清晰,易于维护
类型安全 模板类设计 编译时类型检查
线程安全 原子操作 + 互斥锁 支持高并发场景
动态扩展 incCountTo/decCountTo 按需调整池大小
资源复用 对象预分配 + 回收机制 减少内存分配开销

三、核心组件实现详解

3.1 objPool - 对象池主类

核心数据结构:

cpp 复制代码
template< typename TYPE >
class objPool {
public:
    using type_t = typename std::remove_pointer<
        typename std::remove_all_extents<TYPE>::type>::type;
    
private:
    std::shared_ptr<memPool<uint8_t>> pt_pool__;  // 底层内存池
    objPoolAllocator<type_t>           m_allocator__;  // 对象分配器
    std::list<type_t*>                 m_unused_list__;  // 空闲对象列表
    size_t                             m_max_count__;  // 最大对象数
    std::atomic_flag                   m_lock__;  // 原子锁
};

对象获取流程:
objPoolAllocator m_unused_list__ objPool 应用程序 objPoolAllocator m_unused_list__ objPool 应用程序 #mermaid-svg-fLsy8bVLju1xN5R3{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-fLsy8bVLju1xN5R3 .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-fLsy8bVLju1xN5R3 .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-fLsy8bVLju1xN5R3 .error-icon{fill:#552222;}#mermaid-svg-fLsy8bVLju1xN5R3 .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-fLsy8bVLju1xN5R3 .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-fLsy8bVLju1xN5R3 .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-fLsy8bVLju1xN5R3 .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-fLsy8bVLju1xN5R3 .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-fLsy8bVLju1xN5R3 .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-fLsy8bVLju1xN5R3 .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-fLsy8bVLju1xN5R3 .marker{fill:#333333;stroke:#333333;}#mermaid-svg-fLsy8bVLju1xN5R3 .marker.cross{stroke:#333333;}#mermaid-svg-fLsy8bVLju1xN5R3 svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-fLsy8bVLju1xN5R3 p{margin:0;}#mermaid-svg-fLsy8bVLju1xN5R3 .actor{stroke:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);fill:#ECECFF;}#mermaid-svg-fLsy8bVLju1xN5R3 text.actor>tspan{fill:black;stroke:none;}#mermaid-svg-fLsy8bVLju1xN5R3 .actor-line{stroke:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);}#mermaid-svg-fLsy8bVLju1xN5R3 .innerArc{stroke-width:1.5;stroke-dasharray:none;}#mermaid-svg-fLsy8bVLju1xN5R3 .messageLine0{stroke-width:1.5;stroke-dasharray:none;stroke:#333;}#mermaid-svg-fLsy8bVLju1xN5R3 .messageLine1{stroke-width:1.5;stroke-dasharray:2,2;stroke:#333;}#mermaid-svg-fLsy8bVLju1xN5R3 #arrowhead path{fill:#333;stroke:#333;}#mermaid-svg-fLsy8bVLju1xN5R3 .sequenceNumber{fill:white;}#mermaid-svg-fLsy8bVLju1xN5R3 #sequencenumber{fill:#333;}#mermaid-svg-fLsy8bVLju1xN5R3 #crosshead path{fill:#333;stroke:#333;}#mermaid-svg-fLsy8bVLju1xN5R3 .messageText{fill:#333;stroke:none;}#mermaid-svg-fLsy8bVLju1xN5R3 .labelBox{stroke:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);fill:#ECECFF;}#mermaid-svg-fLsy8bVLju1xN5R3 .labelText,#mermaid-svg-fLsy8bVLju1xN5R3 .labelText>tspan{fill:black;stroke:none;}#mermaid-svg-fLsy8bVLju1xN5R3 .loopText,#mermaid-svg-fLsy8bVLju1xN5R3 .loopText>tspan{fill:black;stroke:none;}#mermaid-svg-fLsy8bVLju1xN5R3 .loopLine{stroke-width:2px;stroke-dasharray:2,2;stroke:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);fill:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);}#mermaid-svg-fLsy8bVLju1xN5R3 .note{stroke:#aaaa33;fill:#fff5ad;}#mermaid-svg-fLsy8bVLju1xN5R3 .noteText,#mermaid-svg-fLsy8bVLju1xN5R3 .noteText>tspan{fill:black;stroke:none;}#mermaid-svg-fLsy8bVLju1xN5R3 .activation0{fill:#f4f4f4;stroke:#666;}#mermaid-svg-fLsy8bVLju1xN5R3 .activation1{fill:#f4f4f4;stroke:#666;}#mermaid-svg-fLsy8bVLju1xN5R3 .activation2{fill:#f4f4f4;stroke:#666;}#mermaid-svg-fLsy8bVLju1xN5R3 .actorPopupMenu{position:absolute;}#mermaid-svg-fLsy8bVLju1xN5R3 .actorPopupMenuPanel{position:absolute;fill:#ECECFF;box-shadow:0px 8px 16px 0px rgba(0,0,0,0.2);filter:drop-shadow(3px 5px 2px rgb(0 0 0 / 0.4));}#mermaid-svg-fLsy8bVLju1xN5R3 .actor-man line{stroke:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);fill:#ECECFF;}#mermaid-svg-fLsy8bVLju1xN5R3 .actor-man circle,#mermaid-svg-fLsy8bVLju1xN5R3 line{stroke:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);fill:#ECECFF;stroke-width:2px;}#mermaid-svg-fLsy8bVLju1xN5R3 :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;} alt 未达到最大容量 已达到最大容量 alt 空闲列表非空 空闲列表为空 get() 获取原子锁 检查空闲列表 返回第一个空闲对象 从列表移除对象 释放原子锁 返回对象指针 检查是否达到最大容量 allocate() 分配内存并构造对象 返回新对象 释放原子锁 返回对象指针 释放原子锁 返回 nullptr

关键方法实现:

cpp 复制代码
type_t* get() {
    // 获取原子锁
    while (m_lock__.test_and_set(std::memory_order_acquire)) {
        std::this_thread::yield();
    }
    
    type_t* ret = nullptr;
    
    if (!m_unused_list__.empty()) {
        // 从空闲列表获取
        ret = m_unused_list__.front();
        m_unused_list__.pop_front();
    } else if (m_used_count__ < m_max_count__) {
        // 分配新对象
        ret = m_allocator__.allocate();
        ++m_used_count__;
    }
    
    m_lock__.clear(std::memory_order_release);
    return ret;
}

3.2 objPoolAllocator - 对象分配器

设计目的: 桥接对象池和内存池,负责对象的内存分配和构造。

cpp 复制代码
template< typename TYPE >
class objPoolAllocator {
private:
    std::shared_ptr<memPool<uint8_t>> pt_pool__;
    
public:
    type_t* allocate() {
        size_t size = sizeof(type_t);
        void* mem = pt_pool__->malloc(size);
        if (mem) {
            // 定位new:在已分配内存上构造对象
            return new(mem) type_t();
        }
        return nullptr;
    }
    
    void deallocate(type_t* p) {
        if (p) {
            // 显式调用析构函数
            p->~type_t();
            pt_pool__->free(sizeof(type_t), p);
        }
    }
};

关键技术点:

  • 定位 new:在预分配的内存上构造对象,避免额外的内存分配
  • 显式析构:手动调用析构函数,配合内存池回收

3.3 memPool - 内存池

核心职责: 管理不同大小的内存块集合。
#mermaid-svg-KwrcZumKUfr1gAr2{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-KwrcZumKUfr1gAr2 .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-KwrcZumKUfr1gAr2 .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-KwrcZumKUfr1gAr2 .error-icon{fill:#552222;}#mermaid-svg-KwrcZumKUfr1gAr2 .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-KwrcZumKUfr1gAr2 .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-KwrcZumKUfr1gAr2 .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-KwrcZumKUfr1gAr2 .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-KwrcZumKUfr1gAr2 .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-KwrcZumKUfr1gAr2 .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-KwrcZumKUfr1gAr2 .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-KwrcZumKUfr1gAr2 .marker{fill:#333333;stroke:#333333;}#mermaid-svg-KwrcZumKUfr1gAr2 .marker.cross{stroke:#333333;}#mermaid-svg-KwrcZumKUfr1gAr2 svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-KwrcZumKUfr1gAr2 p{margin:0;}#mermaid-svg-KwrcZumKUfr1gAr2 g.classGroup text{fill:#9370DB;stroke:none;font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:10px;}#mermaid-svg-KwrcZumKUfr1gAr2 g.classGroup text .title{font-weight:bolder;}#mermaid-svg-KwrcZumKUfr1gAr2 .cluster-label text{fill:#333;}#mermaid-svg-KwrcZumKUfr1gAr2 .cluster-label span{color:#333;}#mermaid-svg-KwrcZumKUfr1gAr2 .cluster-label span p{background-color:transparent;}#mermaid-svg-KwrcZumKUfr1gAr2 .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-KwrcZumKUfr1gAr2 .cluster text{fill:#333;}#mermaid-svg-KwrcZumKUfr1gAr2 .cluster span{color:#333;}#mermaid-svg-KwrcZumKUfr1gAr2 .nodeLabel,#mermaid-svg-KwrcZumKUfr1gAr2 .edgeLabel{color:#131300;}#mermaid-svg-KwrcZumKUfr1gAr2 .edgeLabel .label rect{fill:#ECECFF;}#mermaid-svg-KwrcZumKUfr1gAr2 .label text{fill:#131300;}#mermaid-svg-KwrcZumKUfr1gAr2 .labelBkg{background:#ECECFF;}#mermaid-svg-KwrcZumKUfr1gAr2 .edgeLabel .label span{background:#ECECFF;}#mermaid-svg-KwrcZumKUfr1gAr2 .classTitle{font-weight:bolder;}#mermaid-svg-KwrcZumKUfr1gAr2 .node rect,#mermaid-svg-KwrcZumKUfr1gAr2 .node circle,#mermaid-svg-KwrcZumKUfr1gAr2 .node ellipse,#mermaid-svg-KwrcZumKUfr1gAr2 .node polygon,#mermaid-svg-KwrcZumKUfr1gAr2 .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-KwrcZumKUfr1gAr2 .divider{stroke:#9370DB;stroke-width:1;}#mermaid-svg-KwrcZumKUfr1gAr2 g.clickable{cursor:pointer;}#mermaid-svg-KwrcZumKUfr1gAr2 g.classGroup rect{fill:#ECECFF;stroke:#9370DB;}#mermaid-svg-KwrcZumKUfr1gAr2 g.classGroup line{stroke:#9370DB;stroke-width:1;}#mermaid-svg-KwrcZumKUfr1gAr2 .classLabel .box{stroke:none;stroke-width:0;fill:#ECECFF;opacity:0.5;}#mermaid-svg-KwrcZumKUfr1gAr2 .classLabel .label{fill:#9370DB;font-size:10px;}#mermaid-svg-KwrcZumKUfr1gAr2 .relation{stroke:#333333;stroke-width:1;fill:none;}#mermaid-svg-KwrcZumKUfr1gAr2 .dashed-line{stroke-dasharray:3;}#mermaid-svg-KwrcZumKUfr1gAr2 .dotted-line{stroke-dasharray:1 2;}#mermaid-svg-KwrcZumKUfr1gAr2 #compositionStart,#mermaid-svg-KwrcZumKUfr1gAr2 .composition{fill:#333333!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-KwrcZumKUfr1gAr2 #compositionEnd,#mermaid-svg-KwrcZumKUfr1gAr2 .composition{fill:#333333!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-KwrcZumKUfr1gAr2 #dependencyStart,#mermaid-svg-KwrcZumKUfr1gAr2 .dependency{fill:#333333!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-KwrcZumKUfr1gAr2 #dependencyStart,#mermaid-svg-KwrcZumKUfr1gAr2 .dependency{fill:#333333!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-KwrcZumKUfr1gAr2 #extensionStart,#mermaid-svg-KwrcZumKUfr1gAr2 .extension{fill:transparent!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-KwrcZumKUfr1gAr2 #extensionEnd,#mermaid-svg-KwrcZumKUfr1gAr2 .extension{fill:transparent!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-KwrcZumKUfr1gAr2 #aggregationStart,#mermaid-svg-KwrcZumKUfr1gAr2 .aggregation{fill:transparent!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-KwrcZumKUfr1gAr2 #aggregationEnd,#mermaid-svg-KwrcZumKUfr1gAr2 .aggregation{fill:transparent!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-KwrcZumKUfr1gAr2 #lollipopStart,#mermaid-svg-KwrcZumKUfr1gAr2 .lollipop{fill:#ECECFF!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-KwrcZumKUfr1gAr2 #lollipopEnd,#mermaid-svg-KwrcZumKUfr1gAr2 .lollipop{fill:#ECECFF!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-KwrcZumKUfr1gAr2 .edgeTerminals{font-size:11px;line-height:initial;}#mermaid-svg-KwrcZumKUfr1gAr2 .classTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-KwrcZumKUfr1gAr2 .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-KwrcZumKUfr1gAr2 .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-KwrcZumKUfr1gAr2 :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;} contains
1
*
memPool

  • m_ponds__: map<size, pond>
  • m_lock__: atomic_flag
  • malloc(size) : : void
  • free(size, ptr) : : void
  • registerType(size, count) : : void
    pond
  • m_unused__: set<drop_t>
  • m_used__: set<drop_t>
  • m_size__: size_t
  • malloc() : : drop_t
  • free(drop_t) : : void

内存分配实现:

cpp 复制代码
void* malloc(size_t size) {
    while (m_lock__.test_and_set(std::memory_order_acquire)) {
        std::this_thread::yield();
    }
    
    void* ret = nullptr;
    auto it = m_ponds__.find(size);
    if (it != m_ponds__.end()) {
        ret = it->second->malloc();
    }
    
    m_lock__.clear(std::memory_order_release);
    return ret;
}

3.4 pond - 内存块管理

设计原理: 管理固定大小的内存块,采用位图或链表跟踪使用状态。

cpp 复制代码
template< typename TYPE >
class pond {
private:
    using drop_t = TYPE*;
    
    std::unordered_set<drop_t> m_unused__;  // 空闲块集合
    std::unordered_set<drop_t> m_used__;     // 已使用块集合
    size_t m_count__;  // 总块数
    size_t m_size__;   // 块大小
    
public:
    drop_t malloc() {
        if (m_unused__.empty()) {
            // 自动扩容:翻倍
            reCount(m_count__ * 2);
        }
        
        auto it = m_unused__.begin();
        drop_t ret = *it;
        m_unused__.erase(it);
        m_used__.insert(ret);
        
        return ret;
    }
    
    void free(drop_t p) {
        auto it = m_used__.find(p);
        if (it != m_used__.end()) {
            m_used__.erase(it);
            m_unused__.insert(p);
        }
    }
};

四、对象池工作流程

4.1 对象获取流程

#mermaid-svg-7n84WEEYUbnHw2IV{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-7n84WEEYUbnHw2IV .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-7n84WEEYUbnHw2IV .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-7n84WEEYUbnHw2IV .error-icon{fill:#552222;}#mermaid-svg-7n84WEEYUbnHw2IV .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-7n84WEEYUbnHw2IV .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-7n84WEEYUbnHw2IV .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-7n84WEEYUbnHw2IV .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-7n84WEEYUbnHw2IV .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-7n84WEEYUbnHw2IV .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-7n84WEEYUbnHw2IV .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-7n84WEEYUbnHw2IV .marker{fill:#333333;stroke:#333333;}#mermaid-svg-7n84WEEYUbnHw2IV .marker.cross{stroke:#333333;}#mermaid-svg-7n84WEEYUbnHw2IV svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-7n84WEEYUbnHw2IV p{margin:0;}#mermaid-svg-7n84WEEYUbnHw2IV .label{font-family:"trebuchet ms",verdana,arial,sans-serif;color:#333;}#mermaid-svg-7n84WEEYUbnHw2IV .cluster-label text{fill:#333;}#mermaid-svg-7n84WEEYUbnHw2IV .cluster-label span{color:#333;}#mermaid-svg-7n84WEEYUbnHw2IV .cluster-label span p{background-color:transparent;}#mermaid-svg-7n84WEEYUbnHw2IV .label text,#mermaid-svg-7n84WEEYUbnHw2IV span{fill:#333;color:#333;}#mermaid-svg-7n84WEEYUbnHw2IV .node rect,#mermaid-svg-7n84WEEYUbnHw2IV .node circle,#mermaid-svg-7n84WEEYUbnHw2IV .node ellipse,#mermaid-svg-7n84WEEYUbnHw2IV .node polygon,#mermaid-svg-7n84WEEYUbnHw2IV .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-7n84WEEYUbnHw2IV .rough-node .label text,#mermaid-svg-7n84WEEYUbnHw2IV .node .label text,#mermaid-svg-7n84WEEYUbnHw2IV .image-shape .label,#mermaid-svg-7n84WEEYUbnHw2IV .icon-shape .label{text-anchor:middle;}#mermaid-svg-7n84WEEYUbnHw2IV .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-7n84WEEYUbnHw2IV .rough-node .label,#mermaid-svg-7n84WEEYUbnHw2IV .node .label,#mermaid-svg-7n84WEEYUbnHw2IV .image-shape .label,#mermaid-svg-7n84WEEYUbnHw2IV .icon-shape .label{text-align:center;}#mermaid-svg-7n84WEEYUbnHw2IV .node.clickable{cursor:pointer;}#mermaid-svg-7n84WEEYUbnHw2IV .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-7n84WEEYUbnHw2IV .arrowheadPath{fill:#333333;}#mermaid-svg-7n84WEEYUbnHw2IV .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-7n84WEEYUbnHw2IV .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-7n84WEEYUbnHw2IV .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-7n84WEEYUbnHw2IV .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-7n84WEEYUbnHw2IV .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-7n84WEEYUbnHw2IV .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-7n84WEEYUbnHw2IV .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-7n84WEEYUbnHw2IV .cluster text{fill:#333;}#mermaid-svg-7n84WEEYUbnHw2IV .cluster span{color:#333;}#mermaid-svg-7n84WEEYUbnHw2IV div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-7n84WEEYUbnHw2IV .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-7n84WEEYUbnHw2IV rect.text{fill:none;stroke-width:0;}#mermaid-svg-7n84WEEYUbnHw2IV .icon-shape,#mermaid-svg-7n84WEEYUbnHw2IV .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-7n84WEEYUbnHw2IV .icon-shape p,#mermaid-svg-7n84WEEYUbnHw2IV .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-7n84WEEYUbnHw2IV .icon-shape .label rect,#mermaid-svg-7n84WEEYUbnHw2IV .image-shape .label rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-7n84WEEYUbnHw2IV .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-7n84WEEYUbnHw2IV .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-7n84WEEYUbnHw2IV :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;} 是



应用程序请求对象
空闲列表非空?
从空闲列表取出
未达最大容量?
分配新内存
返回nullptr
定位new构造对象
返回对象指针
返回空指针

4.2 对象释放流程

#mermaid-svg-rZ6uYIxgC81ir9LF{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-rZ6uYIxgC81ir9LF .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-rZ6uYIxgC81ir9LF .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-rZ6uYIxgC81ir9LF .error-icon{fill:#552222;}#mermaid-svg-rZ6uYIxgC81ir9LF .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-rZ6uYIxgC81ir9LF .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-rZ6uYIxgC81ir9LF .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-rZ6uYIxgC81ir9LF .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-rZ6uYIxgC81ir9LF .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-rZ6uYIxgC81ir9LF .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-rZ6uYIxgC81ir9LF .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-rZ6uYIxgC81ir9LF .marker{fill:#333333;stroke:#333333;}#mermaid-svg-rZ6uYIxgC81ir9LF .marker.cross{stroke:#333333;}#mermaid-svg-rZ6uYIxgC81ir9LF svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-rZ6uYIxgC81ir9LF p{margin:0;}#mermaid-svg-rZ6uYIxgC81ir9LF .label{font-family:"trebuchet ms",verdana,arial,sans-serif;color:#333;}#mermaid-svg-rZ6uYIxgC81ir9LF .cluster-label text{fill:#333;}#mermaid-svg-rZ6uYIxgC81ir9LF .cluster-label span{color:#333;}#mermaid-svg-rZ6uYIxgC81ir9LF .cluster-label span p{background-color:transparent;}#mermaid-svg-rZ6uYIxgC81ir9LF .label text,#mermaid-svg-rZ6uYIxgC81ir9LF span{fill:#333;color:#333;}#mermaid-svg-rZ6uYIxgC81ir9LF .node rect,#mermaid-svg-rZ6uYIxgC81ir9LF .node circle,#mermaid-svg-rZ6uYIxgC81ir9LF .node ellipse,#mermaid-svg-rZ6uYIxgC81ir9LF .node polygon,#mermaid-svg-rZ6uYIxgC81ir9LF .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-rZ6uYIxgC81ir9LF .rough-node .label text,#mermaid-svg-rZ6uYIxgC81ir9LF .node .label text,#mermaid-svg-rZ6uYIxgC81ir9LF .image-shape .label,#mermaid-svg-rZ6uYIxgC81ir9LF .icon-shape .label{text-anchor:middle;}#mermaid-svg-rZ6uYIxgC81ir9LF .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-rZ6uYIxgC81ir9LF .rough-node .label,#mermaid-svg-rZ6uYIxgC81ir9LF .node .label,#mermaid-svg-rZ6uYIxgC81ir9LF .image-shape .label,#mermaid-svg-rZ6uYIxgC81ir9LF .icon-shape .label{text-align:center;}#mermaid-svg-rZ6uYIxgC81ir9LF .node.clickable{cursor:pointer;}#mermaid-svg-rZ6uYIxgC81ir9LF .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-rZ6uYIxgC81ir9LF .arrowheadPath{fill:#333333;}#mermaid-svg-rZ6uYIxgC81ir9LF .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-rZ6uYIxgC81ir9LF .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-rZ6uYIxgC81ir9LF .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-rZ6uYIxgC81ir9LF .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-rZ6uYIxgC81ir9LF .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-rZ6uYIxgC81ir9LF .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-rZ6uYIxgC81ir9LF .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-rZ6uYIxgC81ir9LF .cluster text{fill:#333;}#mermaid-svg-rZ6uYIxgC81ir9LF .cluster span{color:#333;}#mermaid-svg-rZ6uYIxgC81ir9LF div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-rZ6uYIxgC81ir9LF .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-rZ6uYIxgC81ir9LF rect.text{fill:none;stroke-width:0;}#mermaid-svg-rZ6uYIxgC81ir9LF .icon-shape,#mermaid-svg-rZ6uYIxgC81ir9LF .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-rZ6uYIxgC81ir9LF .icon-shape p,#mermaid-svg-rZ6uYIxgC81ir9LF .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-rZ6uYIxgC81ir9LF .icon-shape .label rect,#mermaid-svg-rZ6uYIxgC81ir9LF .image-shape .label rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-rZ6uYIxgC81ir9LF .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-rZ6uYIxgC81ir9LF .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-rZ6uYIxgC81ir9LF :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;} 应用程序归还对象
获取原子锁
调用对象reset方法
加入空闲列表
释放原子锁

4.3 动态调整流程

#mermaid-svg-TMuwPuuavjBgucOX{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-TMuwPuuavjBgucOX .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-TMuwPuuavjBgucOX .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-TMuwPuuavjBgucOX .error-icon{fill:#552222;}#mermaid-svg-TMuwPuuavjBgucOX .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-TMuwPuuavjBgucOX .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-TMuwPuuavjBgucOX .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-TMuwPuuavjBgucOX .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-TMuwPuuavjBgucOX .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-TMuwPuuavjBgucOX .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-TMuwPuuavjBgucOX .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-TMuwPuuavjBgucOX .marker{fill:#333333;stroke:#333333;}#mermaid-svg-TMuwPuuavjBgucOX .marker.cross{stroke:#333333;}#mermaid-svg-TMuwPuuavjBgucOX svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-TMuwPuuavjBgucOX p{margin:0;}#mermaid-svg-TMuwPuuavjBgucOX .label{font-family:"trebuchet ms",verdana,arial,sans-serif;color:#333;}#mermaid-svg-TMuwPuuavjBgucOX .cluster-label text{fill:#333;}#mermaid-svg-TMuwPuuavjBgucOX .cluster-label span{color:#333;}#mermaid-svg-TMuwPuuavjBgucOX .cluster-label span p{background-color:transparent;}#mermaid-svg-TMuwPuuavjBgucOX .label text,#mermaid-svg-TMuwPuuavjBgucOX span{fill:#333;color:#333;}#mermaid-svg-TMuwPuuavjBgucOX .node rect,#mermaid-svg-TMuwPuuavjBgucOX .node circle,#mermaid-svg-TMuwPuuavjBgucOX .node ellipse,#mermaid-svg-TMuwPuuavjBgucOX .node polygon,#mermaid-svg-TMuwPuuavjBgucOX .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-TMuwPuuavjBgucOX .rough-node .label text,#mermaid-svg-TMuwPuuavjBgucOX .node .label text,#mermaid-svg-TMuwPuuavjBgucOX .image-shape .label,#mermaid-svg-TMuwPuuavjBgucOX .icon-shape .label{text-anchor:middle;}#mermaid-svg-TMuwPuuavjBgucOX .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-TMuwPuuavjBgucOX .rough-node .label,#mermaid-svg-TMuwPuuavjBgucOX .node .label,#mermaid-svg-TMuwPuuavjBgucOX .image-shape .label,#mermaid-svg-TMuwPuuavjBgucOX .icon-shape .label{text-align:center;}#mermaid-svg-TMuwPuuavjBgucOX .node.clickable{cursor:pointer;}#mermaid-svg-TMuwPuuavjBgucOX .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-TMuwPuuavjBgucOX .arrowheadPath{fill:#333333;}#mermaid-svg-TMuwPuuavjBgucOX .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-TMuwPuuavjBgucOX .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-TMuwPuuavjBgucOX .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-TMuwPuuavjBgucOX .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-TMuwPuuavjBgucOX .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-TMuwPuuavjBgucOX .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-TMuwPuuavjBgucOX .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-TMuwPuuavjBgucOX .cluster text{fill:#333;}#mermaid-svg-TMuwPuuavjBgucOX .cluster span{color:#333;}#mermaid-svg-TMuwPuuavjBgucOX div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-TMuwPuuavjBgucOX .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-TMuwPuuavjBgucOX rect.text{fill:none;stroke-width:0;}#mermaid-svg-TMuwPuuavjBgucOX .icon-shape,#mermaid-svg-TMuwPuuavjBgucOX .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-TMuwPuuavjBgucOX .icon-shape p,#mermaid-svg-TMuwPuuavjBgucOX .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-TMuwPuuavjBgucOX .icon-shape .label rect,#mermaid-svg-TMuwPuuavjBgucOX .image-shape .label rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-TMuwPuuavjBgucOX .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-TMuwPuuavjBgucOX .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-TMuwPuuavjBgucOX :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;} 是



调用incCountTo/newSize
newSize > 当前大小?
计算需要新增的对象数
无需操作
循环分配新对象
加入空闲列表
达到目标大小?
完成扩展


五、线程安全机制

5.1 同步策略

#mermaid-svg-ZQFS6b8xRWUxKzTG{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-ZQFS6b8xRWUxKzTG .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-ZQFS6b8xRWUxKzTG .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-ZQFS6b8xRWUxKzTG .error-icon{fill:#552222;}#mermaid-svg-ZQFS6b8xRWUxKzTG .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-ZQFS6b8xRWUxKzTG .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-ZQFS6b8xRWUxKzTG .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-ZQFS6b8xRWUxKzTG .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-ZQFS6b8xRWUxKzTG .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-ZQFS6b8xRWUxKzTG .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-ZQFS6b8xRWUxKzTG .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-ZQFS6b8xRWUxKzTG .marker{fill:#333333;stroke:#333333;}#mermaid-svg-ZQFS6b8xRWUxKzTG .marker.cross{stroke:#333333;}#mermaid-svg-ZQFS6b8xRWUxKzTG svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-ZQFS6b8xRWUxKzTG p{margin:0;}#mermaid-svg-ZQFS6b8xRWUxKzTG g.classGroup text{fill:#9370DB;stroke:none;font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:10px;}#mermaid-svg-ZQFS6b8xRWUxKzTG g.classGroup text .title{font-weight:bolder;}#mermaid-svg-ZQFS6b8xRWUxKzTG .cluster-label text{fill:#333;}#mermaid-svg-ZQFS6b8xRWUxKzTG .cluster-label span{color:#333;}#mermaid-svg-ZQFS6b8xRWUxKzTG .cluster-label span p{background-color:transparent;}#mermaid-svg-ZQFS6b8xRWUxKzTG .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-ZQFS6b8xRWUxKzTG .cluster text{fill:#333;}#mermaid-svg-ZQFS6b8xRWUxKzTG .cluster span{color:#333;}#mermaid-svg-ZQFS6b8xRWUxKzTG .nodeLabel,#mermaid-svg-ZQFS6b8xRWUxKzTG .edgeLabel{color:#131300;}#mermaid-svg-ZQFS6b8xRWUxKzTG .edgeLabel .label rect{fill:#ECECFF;}#mermaid-svg-ZQFS6b8xRWUxKzTG .label text{fill:#131300;}#mermaid-svg-ZQFS6b8xRWUxKzTG .labelBkg{background:#ECECFF;}#mermaid-svg-ZQFS6b8xRWUxKzTG .edgeLabel .label span{background:#ECECFF;}#mermaid-svg-ZQFS6b8xRWUxKzTG .classTitle{font-weight:bolder;}#mermaid-svg-ZQFS6b8xRWUxKzTG .node rect,#mermaid-svg-ZQFS6b8xRWUxKzTG .node circle,#mermaid-svg-ZQFS6b8xRWUxKzTG .node ellipse,#mermaid-svg-ZQFS6b8xRWUxKzTG .node polygon,#mermaid-svg-ZQFS6b8xRWUxKzTG .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-ZQFS6b8xRWUxKzTG .divider{stroke:#9370DB;stroke-width:1;}#mermaid-svg-ZQFS6b8xRWUxKzTG g.clickable{cursor:pointer;}#mermaid-svg-ZQFS6b8xRWUxKzTG g.classGroup rect{fill:#ECECFF;stroke:#9370DB;}#mermaid-svg-ZQFS6b8xRWUxKzTG g.classGroup line{stroke:#9370DB;stroke-width:1;}#mermaid-svg-ZQFS6b8xRWUxKzTG .classLabel .box{stroke:none;stroke-width:0;fill:#ECECFF;opacity:0.5;}#mermaid-svg-ZQFS6b8xRWUxKzTG .classLabel .label{fill:#9370DB;font-size:10px;}#mermaid-svg-ZQFS6b8xRWUxKzTG .relation{stroke:#333333;stroke-width:1;fill:none;}#mermaid-svg-ZQFS6b8xRWUxKzTG .dashed-line{stroke-dasharray:3;}#mermaid-svg-ZQFS6b8xRWUxKzTG .dotted-line{stroke-dasharray:1 2;}#mermaid-svg-ZQFS6b8xRWUxKzTG #compositionStart,#mermaid-svg-ZQFS6b8xRWUxKzTG .composition{fill:#333333!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-ZQFS6b8xRWUxKzTG #compositionEnd,#mermaid-svg-ZQFS6b8xRWUxKzTG .composition{fill:#333333!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-ZQFS6b8xRWUxKzTG #dependencyStart,#mermaid-svg-ZQFS6b8xRWUxKzTG .dependency{fill:#333333!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-ZQFS6b8xRWUxKzTG #dependencyStart,#mermaid-svg-ZQFS6b8xRWUxKzTG .dependency{fill:#333333!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-ZQFS6b8xRWUxKzTG #extensionStart,#mermaid-svg-ZQFS6b8xRWUxKzTG .extension{fill:transparent!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-ZQFS6b8xRWUxKzTG #extensionEnd,#mermaid-svg-ZQFS6b8xRWUxKzTG .extension{fill:transparent!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-ZQFS6b8xRWUxKzTG #aggregationStart,#mermaid-svg-ZQFS6b8xRWUxKzTG .aggregation{fill:transparent!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-ZQFS6b8xRWUxKzTG #aggregationEnd,#mermaid-svg-ZQFS6b8xRWUxKzTG .aggregation{fill:transparent!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-ZQFS6b8xRWUxKzTG #lollipopStart,#mermaid-svg-ZQFS6b8xRWUxKzTG .lollipop{fill:#ECECFF!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-ZQFS6b8xRWUxKzTG #lollipopEnd,#mermaid-svg-ZQFS6b8xRWUxKzTG .lollipop{fill:#ECECFF!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-ZQFS6b8xRWUxKzTG .edgeTerminals{font-size:11px;line-height:initial;}#mermaid-svg-ZQFS6b8xRWUxKzTG .classTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-ZQFS6b8xRWUxKzTG .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-ZQFS6b8xRWUxKzTG .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-ZQFS6b8xRWUxKzTG :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;} protects
protects
ThreadSafety

  • atomic_flag: 轻量级原子锁
  • std::mutex: 重量级互斥锁
  • memory_order: 内存序控制
    objPool
  • m_lock__: atomic_flag
    memPool
  • m_lock__: atomic_flag

5.2 锁粒度分析

组件 锁类型 粒度 适用场景
objPool atomic_flag 细粒度 高并发获取/释放
memPool atomic_flag 中粒度 内存分配/释放
pond 无锁 无锁 单线程访问

5.3 性能对比

#mermaid-svg-uJEvWriEBnGxbZQj{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-uJEvWriEBnGxbZQj .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-uJEvWriEBnGxbZQj .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-uJEvWriEBnGxbZQj .error-icon{fill:#552222;}#mermaid-svg-uJEvWriEBnGxbZQj .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-uJEvWriEBnGxbZQj .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-uJEvWriEBnGxbZQj .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-uJEvWriEBnGxbZQj .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-uJEvWriEBnGxbZQj .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-uJEvWriEBnGxbZQj .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-uJEvWriEBnGxbZQj .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-uJEvWriEBnGxbZQj .marker{fill:#333333;stroke:#333333;}#mermaid-svg-uJEvWriEBnGxbZQj .marker.cross{stroke:#333333;}#mermaid-svg-uJEvWriEBnGxbZQj svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-uJEvWriEBnGxbZQj p{margin:0;}#mermaid-svg-uJEvWriEBnGxbZQj .label{font-family:"trebuchet ms",verdana,arial,sans-serif;color:#333;}#mermaid-svg-uJEvWriEBnGxbZQj .cluster-label text{fill:#333;}#mermaid-svg-uJEvWriEBnGxbZQj .cluster-label span{color:#333;}#mermaid-svg-uJEvWriEBnGxbZQj .cluster-label span p{background-color:transparent;}#mermaid-svg-uJEvWriEBnGxbZQj .label text,#mermaid-svg-uJEvWriEBnGxbZQj span{fill:#333;color:#333;}#mermaid-svg-uJEvWriEBnGxbZQj .node rect,#mermaid-svg-uJEvWriEBnGxbZQj .node circle,#mermaid-svg-uJEvWriEBnGxbZQj .node ellipse,#mermaid-svg-uJEvWriEBnGxbZQj .node polygon,#mermaid-svg-uJEvWriEBnGxbZQj .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-uJEvWriEBnGxbZQj .rough-node .label text,#mermaid-svg-uJEvWriEBnGxbZQj .node .label text,#mermaid-svg-uJEvWriEBnGxbZQj .image-shape .label,#mermaid-svg-uJEvWriEBnGxbZQj .icon-shape .label{text-anchor:middle;}#mermaid-svg-uJEvWriEBnGxbZQj .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-uJEvWriEBnGxbZQj .rough-node .label,#mermaid-svg-uJEvWriEBnGxbZQj .node .label,#mermaid-svg-uJEvWriEBnGxbZQj .image-shape .label,#mermaid-svg-uJEvWriEBnGxbZQj .icon-shape .label{text-align:center;}#mermaid-svg-uJEvWriEBnGxbZQj .node.clickable{cursor:pointer;}#mermaid-svg-uJEvWriEBnGxbZQj .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-uJEvWriEBnGxbZQj .arrowheadPath{fill:#333333;}#mermaid-svg-uJEvWriEBnGxbZQj .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-uJEvWriEBnGxbZQj .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-uJEvWriEBnGxbZQj .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-uJEvWriEBnGxbZQj .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-uJEvWriEBnGxbZQj .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-uJEvWriEBnGxbZQj .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-uJEvWriEBnGxbZQj .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-uJEvWriEBnGxbZQj .cluster text{fill:#333;}#mermaid-svg-uJEvWriEBnGxbZQj .cluster span{color:#333;}#mermaid-svg-uJEvWriEBnGxbZQj div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-uJEvWriEBnGxbZQj .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-uJEvWriEBnGxbZQj rect.text{fill:none;stroke-width:0;}#mermaid-svg-uJEvWriEBnGxbZQj .icon-shape,#mermaid-svg-uJEvWriEBnGxbZQj .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-uJEvWriEBnGxbZQj .icon-shape p,#mermaid-svg-uJEvWriEBnGxbZQj .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-uJEvWriEBnGxbZQj .icon-shape .label rect,#mermaid-svg-uJEvWriEBnGxbZQj .image-shape .label rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-uJEvWriEBnGxbZQj .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-uJEvWriEBnGxbZQj .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-uJEvWriEBnGxbZQj :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;} 最快
较快
最慢
无锁
atomic_flag
std::mutex
std::recursive_mutex


六、性能分析与优化

6.1 性能指标

指标 说明 计算公式
命中率 从空闲列表获取的比例 命中次数 / 总获取次数
内存利用率 已使用内存占总分配内存的比例 已使用 / 总分配
分配延迟 单次分配的平均时间 总时间 / 分配次数

6.2 优化策略

#mermaid-svg-QQE8XWFmaWV9sbIs{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-QQE8XWFmaWV9sbIs .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-QQE8XWFmaWV9sbIs .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-QQE8XWFmaWV9sbIs .error-icon{fill:#552222;}#mermaid-svg-QQE8XWFmaWV9sbIs .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-QQE8XWFmaWV9sbIs .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-QQE8XWFmaWV9sbIs .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-QQE8XWFmaWV9sbIs .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-QQE8XWFmaWV9sbIs .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-QQE8XWFmaWV9sbIs .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-QQE8XWFmaWV9sbIs .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-QQE8XWFmaWV9sbIs .marker{fill:#333333;stroke:#333333;}#mermaid-svg-QQE8XWFmaWV9sbIs .marker.cross{stroke:#333333;}#mermaid-svg-QQE8XWFmaWV9sbIs svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-QQE8XWFmaWV9sbIs p{margin:0;}#mermaid-svg-QQE8XWFmaWV9sbIs .label{font-family:"trebuchet ms",verdana,arial,sans-serif;color:#333;}#mermaid-svg-QQE8XWFmaWV9sbIs .cluster-label text{fill:#333;}#mermaid-svg-QQE8XWFmaWV9sbIs .cluster-label span{color:#333;}#mermaid-svg-QQE8XWFmaWV9sbIs .cluster-label span p{background-color:transparent;}#mermaid-svg-QQE8XWFmaWV9sbIs .label text,#mermaid-svg-QQE8XWFmaWV9sbIs span{fill:#333;color:#333;}#mermaid-svg-QQE8XWFmaWV9sbIs .node rect,#mermaid-svg-QQE8XWFmaWV9sbIs .node circle,#mermaid-svg-QQE8XWFmaWV9sbIs .node ellipse,#mermaid-svg-QQE8XWFmaWV9sbIs .node polygon,#mermaid-svg-QQE8XWFmaWV9sbIs .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-QQE8XWFmaWV9sbIs .rough-node .label text,#mermaid-svg-QQE8XWFmaWV9sbIs .node .label text,#mermaid-svg-QQE8XWFmaWV9sbIs .image-shape .label,#mermaid-svg-QQE8XWFmaWV9sbIs .icon-shape .label{text-anchor:middle;}#mermaid-svg-QQE8XWFmaWV9sbIs .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-QQE8XWFmaWV9sbIs .rough-node .label,#mermaid-svg-QQE8XWFmaWV9sbIs .node .label,#mermaid-svg-QQE8XWFmaWV9sbIs .image-shape .label,#mermaid-svg-QQE8XWFmaWV9sbIs .icon-shape .label{text-align:center;}#mermaid-svg-QQE8XWFmaWV9sbIs .node.clickable{cursor:pointer;}#mermaid-svg-QQE8XWFmaWV9sbIs .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-QQE8XWFmaWV9sbIs .arrowheadPath{fill:#333333;}#mermaid-svg-QQE8XWFmaWV9sbIs .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-QQE8XWFmaWV9sbIs .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-QQE8XWFmaWV9sbIs .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-QQE8XWFmaWV9sbIs .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-QQE8XWFmaWV9sbIs .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-QQE8XWFmaWV9sbIs .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-QQE8XWFmaWV9sbIs .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-QQE8XWFmaWV9sbIs .cluster text{fill:#333;}#mermaid-svg-QQE8XWFmaWV9sbIs .cluster span{color:#333;}#mermaid-svg-QQE8XWFmaWV9sbIs div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-QQE8XWFmaWV9sbIs .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-QQE8XWFmaWV9sbIs rect.text{fill:none;stroke-width:0;}#mermaid-svg-QQE8XWFmaWV9sbIs .icon-shape,#mermaid-svg-QQE8XWFmaWV9sbIs .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-QQE8XWFmaWV9sbIs .icon-shape p,#mermaid-svg-QQE8XWFmaWV9sbIs .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-QQE8XWFmaWV9sbIs .icon-shape .label rect,#mermaid-svg-QQE8XWFmaWV9sbIs .image-shape .label rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-QQE8XWFmaWV9sbIs .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-QQE8XWFmaWV9sbIs .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-QQE8XWFmaWV9sbIs :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;} 性能优化
内存局部性优化
锁竞争优化
内存碎片优化
连续内存分配
对象对齐优化
无锁数据结构
线程局部存储
批量操作
固定大小块
内存紧凑

6.3 对比测试结果

渲染错误: Mermaid 渲染失败: No diagram type detected matching given configuration for text: bar title 对象池 vs 直接new性能对比 x-axis "1000次", "10000次", "100000次" y-axis "平均耗时(ms)" bar {"name": "objPool", "values": \[1.2, 8.5, 78.3}, {"name": "new/delete", "values": 3.5, 32.1, 315.6} ]


七、使用示例

7.1 基本使用

cpp 复制代码
#include "objPool.hpp"

class Connection {
public:
    Connection() { /* 初始化连接 */ }
    ~Connection() { /* 清理连接 */ }
    void reset() { /* 重置状态 */ }
    void connect(const std::string& addr) { /* 建立连接 */ }
    void disconnect() { /* 断开连接 */ }
};

int main() {
    // 创建对象池,预分配10个连接
    wheels::objPool<Connection> pool(10);
    
    // 获取连接
    Connection* conn = pool.get();
    if (conn) {
        conn->connect("tcp://localhost:8080");
        // 使用连接...
        
        // 归还连接
        conn->disconnect();
        conn->reset();
        pool.release(conn);
    }
    
    return 0;
}

7.2 配合智能指针

cpp 复制代码
#include "objPool.hpp"
#include <memory>

template<typename T>
using PoolPtr = std::unique_ptr<T, std::function<void(T*)>>;

template<typename T>
PoolPtr<T> getPoolObject(wheels::objPool<T>& pool) {
    T* obj = pool.get();
    if (obj) {
        return PoolPtr<T>(obj, [&pool](T* p) {
            p->reset();
            pool.release(p);
        });
    }
    return nullptr;
}

// 使用
wheels::objPool<Connection> pool(10);
auto conn = getPoolObject(pool);
if (conn) {
    conn->connect("tcp://localhost:8080");
    // 自动归还...
}

7.3 多线程场景

cpp 复制代码
#include "objPool.hpp"
#include <thread>
#include <vector>

void worker(wheels::objPool<Connection>& pool, int id) {
    for (int i = 0; i < 100; ++i) {
        auto conn = getPoolObject(pool);
        if (conn) {
            conn->connect("tcp://localhost:8080");
            // 执行任务...
        }
    }
}

int main() {
    wheels::objPool<Connection> pool(10);
    
    std::vector<std::thread> threads;
    for (int i = 0; i < 8; ++i) {
        threads.emplace_back(worker, std::ref(pool), i);
    }
    
    for (auto& t : threads) {
        t.join();
    }
    
    return 0;
}

八、常见问题与解决方案

8.1 对象状态污染

问题:归还的对象可能保留上次使用的状态。

解决方案

cpp 复制代码
void release(type_t* p) {
    // 获取锁
    while (m_lock__.test_and_set(std::memory_order_acquire)) {
        std::this_thread::yield();
    }
    
    // 调用reset方法重置状态
    p->reset();
    
    // 加入空闲列表
    m_unused_list__.push_back(p);
    
    m_lock__.clear(std::memory_order_release);
}

8.2 内存泄漏

问题:对象池持有内存,即使对象不再使用也不释放。

解决方案

cpp 复制代码
void clear() {
    while (m_lock__.test_and_set(std::memory_order_acquire)) {
        std::this_thread::yield();
    }
    
    // 销毁所有对象
    for (auto p : m_unused_list__) {
        m_allocator__.deallocate(p);
    }
    m_unused_list__.clear();
    
    m_lock__.clear(std::memory_order_release);
}

8.3 死锁风险

问题:嵌套调用可能导致死锁。

解决方案

  • 使用非阻塞原子操作
  • 避免在持有锁时调用外部函数
  • 使用 std::lock_guard 自动管理锁

九、总结

9.1 架构亮点

#mermaid-svg-sS4KmaH5fC7skiTF{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-sS4KmaH5fC7skiTF .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-sS4KmaH5fC7skiTF .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-sS4KmaH5fC7skiTF .error-icon{fill:#552222;}#mermaid-svg-sS4KmaH5fC7skiTF .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-sS4KmaH5fC7skiTF .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-sS4KmaH5fC7skiTF .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-sS4KmaH5fC7skiTF .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-sS4KmaH5fC7skiTF .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-sS4KmaH5fC7skiTF .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-sS4KmaH5fC7skiTF .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-sS4KmaH5fC7skiTF .marker{fill:#333333;stroke:#333333;}#mermaid-svg-sS4KmaH5fC7skiTF .marker.cross{stroke:#333333;}#mermaid-svg-sS4KmaH5fC7skiTF svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-sS4KmaH5fC7skiTF p{margin:0;}#mermaid-svg-sS4KmaH5fC7skiTF .pieCircle{stroke:#000000;stroke-width:2px;opacity:0.7;}#mermaid-svg-sS4KmaH5fC7skiTF .pieOuterCircle{stroke:#000000;stroke-width:1px;fill:none;}#mermaid-svg-sS4KmaH5fC7skiTF .pieTitleText{text-anchor:middle;font-size:25px;fill:#000000;font-family:"trebuchet ms",verdana,arial,sans-serif;}#mermaid-svg-sS4KmaH5fC7skiTF .slice{font-family:"trebuchet ms",verdana,arial,sans-serif;fill:#000000;font-size:17px;}#mermaid-svg-sS4KmaH5fC7skiTF .legend text{fill:#000000;font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:17px;}#mermaid-svg-sS4KmaH5fC7skiTF :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;} 40% 25% 20% 15% 架构优势分布 性能优化 线程安全 可扩展性 易用性

9.2 适用场景总结

场景 推荐使用 原因
高频对象创建销毁 显著减少分配开销
内存敏感应用 减少内存碎片
高并发环境 线程安全设计
对象初始化成本高 复用避免重复初始化
对象生命周期短 池化收益最大

9.3 未来改进方向

  1. 无锁数据结构:进一步降低锁竞争
  2. 动态收缩:根据负载自动释放空闲对象
  3. 统计监控:添加使用统计和监控接口
  4. 对象验证:归还时验证对象有效性
  5. 自定义分配策略:支持多种分配策略

附录:核心API速查

方法 功能 返回值
objPool(size_t max) 构造函数 -
get() 获取对象 对象指针/nullptr
release(T*) 归还对象 void
incCountTo(size_t) 扩展池大小 bool
decCountTo(size_t) 收缩池大小 bool
clear() 清空对象池 void
size() 获取池大小 size_t
usedCount() 获取已使用数 size_t

本文详细介绍了 wheels::objPool 的架构设计、实现原理和使用方法。对象池模式在高性能应用中扮演着重要角色,合理使用可以显著提升系统性能和稳定性。 🚀

相关推荐
多彩电脑1 小时前
Swift里字符串的索引
开发语言·swift
SoftLipaRZC1 小时前
C语言预处理详解:从宏定义到条件编译
c语言·开发语言
lilili也1 小时前
C++:类
c++
L_09071 小时前
【C++】智能指针
开发语言·c++·智能指针
程序猿乐锅1 小时前
【苍穹外卖|Day02】后台接口自测闭环:Token、DTO 与 yml 配置
java·开发语言
春栀怡铃声1 小时前
【C++修仙录02】筑基篇:适配器
c++
冰暮流星1 小时前
javascript之对象的建立-使用Object
开发语言·javascript·ecmascript
融智兴科技1 小时前
防伪强度从哪里来?一物一码、动态验证与后台风控
大数据·科技·物联网
qq_2518364571 小时前
基于java 税务管理系统设计与实现
java·开发语言