C++对象池以及shared_ptr的支持

对象池 和 支持对象池的shared_ptr

性能测试

数字的单位是微妙 分配5万个 1kb的object 

10 kb

100kb

500b

128b

因此 只考虑对象分配速度问题的话  大于 512的对象推荐使用pool版本

第一个是:自己实现的池容器  对象池

第二个是:make_shared   非对象池

第三个是:原始的new delete  非对象池

测试2:

和上上面的一样 只不过第一条数据是 std::queue 而非自己实现的容器

对于release 0x3优化 版本的测试 相差很大

 

32bytes

64bytes

128 bytes

512

10kb

因此综合来看 超过 32 字节的 都应该用pool来处理

Object.h

1#pragma once 2/* 3Email me@dreamyouxi.com 4 5注意事项: 61.使用例子在cpp文件各种test函数里面 72. 8 9class说明: 10RawObject: 111.最原始的指针 只不过封装了一个release函数 用于delete this 12 13RefObject: 141.简单的引用计数 需要手动去计数 Retain Release快速简单 15 16RefObjectThreadSafe: 171.RefObject的线程安全版本 只保证计数安全 18 19SharedObject: 201.std::enable_shared_from_this 封装 无需手动在继承 如果想用原始的std::shared_ptr可以继承方便使用 21 22Recycleable: 231.对象池可回收对象 ctor和dtor需要重写 以供初始化内部数据 24 25share_ptr: 261.ShareObjectPool 专有的指针类型 内部封装了std::shared_ptr,必需配合ShareObjectPool 使用 27 28 29对象池: 30ObjectPool : 311.64字节以上的class 都应该使用pool 322.对象池管理的对象必需是 RawObjectRecycleable OR RefObjectRecycleable OR Recycleable(需要手动调用ObjectPool::Release) 的子类 333.对于子类来说必需提供默认无参数构造函数public,对于构造方法请用其他方法(ctor or other member function)实现 而非依托于构造函数 34 35ShareObjectPool: 361.64字节以上的class 都应该使用pool 372.该对象池相比ObjectPool 是share_ptr的专有对象池 利用share_ptr来管理对象的生命周期 383.管理的对象必需是 ShareObjectRecycleable 的子类 394.对于子类来说必需提供默认无参数构造函数public,对于构造方法请用其他方法(ctor or other member function)实现 而非依托于构造函数 40 41TODO: 421.考虑是否不让new跑出异常而继续程序运行 new(std::nothrow)XX; 43*/ 44#include <memory> 45#include <atomic> 46#include <mutex> 47 48class Object 49{ 50public: 51}; 52//raw pointer object 53class RawObject 54{ 55public: 56 /** 57 * @brief call this to delete object 58 */ 59 inline virtual void Release() 60 { 61 delete this; 62 } 63 virtual ~RawObject() 64 { 65 66 } 67}; 68//reference counter object 69//this class is none-thread-safe, thread-safe replacer is (std::shared_ptr) or RefObjectThreadSafe 70class RefObject 71{ 72protected: 73 int referenct_count = 1; 74public: 75 /** 76 * @brief add reference count 77 */ 78 inline void Retain() 79 { 80 ++referenct_count; 81 } 82 /** 83 * @brief reduce reference count 84 */ 85 inline virtual void Release() 86 { 87 --referenct_count; 88 if (referenct_count <= 0) 89 { 90 delete this; 91 } 92 } 93 inline int GetReferenceCount()const 94 { 95 return referenct_count; 96 } 97 virtual ~RefObject() 98 { 99 100 } 101 //TODO add this to auto release pool or add std::shared_ptr suport 102 103}; 104//reference counter object 105class RefObjectThreadSafe 106{ 107protected: 108 std::atomic<int> referenct_count = 1; 109public: 110 /** 111 * @brief add reference count 112 */ 113 inline void Retain() 114 { 115 ++referenct_count; 116 } 117 /** 118 * @brief reduce reference count 119 */ 120 inline virtual void Release() 121 { 122 --referenct_count; 123 if (referenct_count == 0) 124 { 125 delete this; 126 } 127 } 128 inline int GetReferenceCount()const 129 { 130 return referenct_count; 131 } 132 virtual ~RefObjectThreadSafe() 133 { 134 135 } 136 //TODO add this to auto release pool or add std::shared_ptr suport 137 138}; 139 140//raw share ptr wrapper class 141//create by std::make_shared ,pass object by share_from_this or std::shared_ptr's method 142template<typename SubClassType> 143class SharedObject :public std::enable_shared_from_this<SubClassType> 144{ 145public: 146 virtual ~SharedObject() 147 { 148 149 } 150 //xxx->share_from_this(); 151}; 152 153//for object pool 154class Recycleable 155{ 156public: 157 /** 158 * @brief when re-use this class this will be call 159 */ 160 virtual void ctor() = 0; 161 /** 162 * @brief when recycle by pool this will be call 163 */ 164 virtual void dtor() = 0; 165 //tag for class ObjectPool 166 //泛型约束 167 inline void ___type_sub_class_restrain__FOR_ObjectPool() 168 { 169 } 170}; 171 172class NoneRecycleable 173{ 174public: 175 void ctor() = delete; 176 void dtor() = delete; 177}; 178 179template< class T, bool ThreadSafe> 180class share_ptr; 181 182//对象>=该大小时才会使用Pool 否则Pool会动态new 和delete 183#define OBJECT_POOL_MIN_SIZEOF 32 184//40KB 1w objects 185#define OBJECT_POOL_MAX_SIZE 10240 186//1Kb 为对象池 预分配的 队列空间 的个数大小 187#define OBJECT_POOL_DEFAULT_ALLOC_SIZE 512 188//对象池预分配对象大小 必需小于 OBJECT_POOL_DEFAULT_ALLOC_SIZE 189#define OBJECT_POOL_DEFAULT_ALLOC_OBJECT_SIZE 256 190 191template< typename ClassType, typename bool ThreadSafe = false> 192class ObjectPool 193{ 194 template< class ClassType, bool ThreadSafe = false> 195 class Ctor 196 { 197 public: 198 Ctor() 199 {//ctor 200 int capacity = ObjectPool<ClassType, ThreadSafe>::capacity; 201 auto pool = ObjectPool<ClassType, ThreadSafe>::_pool; 202 for (int i = 0; i < capacity; i++) 203 { 204 pool[i] = nullptr; 205 } 206 } 207 ~Ctor() 208 {//dtor 209 ObjectPool<ClassType, ThreadSafe>::Clear(); 210 //如果编译报错 那么证明不是约束下的 子类关系 请确认 211 ClassType * s = nullptr; 212 s->___type_sub_class_restrain__FOR_ObjectPool(); 213 } 214 }; 215 ObjectPool(); 216 static int capacity; 217 static ClassType** _pool; 218 static int size; 219 static std::mutex _mutex; 220private: 221 /** 222 * @brief resize inner pool growing by twice 223 * @warning this will copy old block and delete old 224 */ 225 static void ReSize() 226 { 227 auto pool = new ClassType*[capacity * 2];// 228 memset(pool, 0, sizeof(ClassType*)* 2 * capacity); 229 memcpy(pool, _pool, sizeof(ClassType*)* capacity); 230 capacity *= 2; 231 delete _pool; 232 _pool = pool; 233 } 234 static void PreAlloc() 235 { 236 auto pool = _pool; 237 for (int i = 0; i < OBJECT_POOL_DEFAULT_ALLOC_OBJECT_SIZE; i++) 238 { 239 pool[size] = new ClassType(); 240 ++size; 241 } 242 } 243public: 244 /** 245 * @brief call this to new class who is the sub-class of Recycleable 246 * @note if class size if smaller than OBJECT_POOL_MIN_SIZEOF will new otherwise will gen object from pool 247 */ 248 static ClassType* Create() 249 { 250 if (ThreadSafe) 251 { 252 ClassType * ret = nullptr; 253 { 254 std::lock_guard<std::mutex> locker(_mutex); 255 static ObjectPool<ClassType, ThreadSafe>::Ctor<ClassType, ThreadSafe> ctor;//c11 compiler suport thread-safe 256 if (size > 0 && sizeof(ClassType) >= OBJECT_POOL_MIN_SIZEOF) 257 { 258 --size; 259 ret = (_pool[size]); 260 _pool[size] = nullptr; 261 } 262 } 263 if (!ret) 264 { 265 ret = new ClassType(); 266 } 267 ret->ctor(); 268 return ret; 269 } 270 else 271 { 272 //ctor之所以弄为函数的static是因为全局的static析构并不完全和定义顺序一致 因为为了避免不同编译器下出错就放在这里了 273 //局部的肯定比全局的先析构 274 static ObjectPool<ClassType, ThreadSafe>::Ctor<ClassType, ThreadSafe> ctor;//c11 compiler suport thread-safe 275 if (size > 0 && sizeof(ClassType) >= OBJECT_POOL_MIN_SIZEOF) 276 { 277 --size; 278 ClassType* ret = (_pool[size]); 279 _pool[size] = nullptr; 280 ret->ctor(); 281 return ret; 282 } 283 auto ret = new ClassType(); 284 ret->ctor(); 285 return ret; 286 } 287 } 288 /** 289 * @brief release object to the pool 290 * @note if sizeof class is smaller than OBJECT_POOL_MIN_SIZEOF will delete now otherwise will put into pool 291 */ 292 static void Release(ClassType* who) 293 { 294 if (!who)return; 295 who->dtor(); 296 if (sizeof(ClassType) < OBJECT_POOL_MIN_SIZEOF) 297 { 298 delete who; 299 return; 300 } 301 if (ThreadSafe) 302 { 303 std::lock_guard<std::mutex> locker(_mutex); 304 if (size >= OBJECT_POOL_MAX_SIZE) 305 { 306 cout << capacity << " " << typeid(ObjectPool<ClassType, ThreadSafe>).name() << " warnning Release out-of-size" << endl; 307 delete who; 308 return; 309 } 310 if (size >= capacity) 311 { 312 ReSize(); 313 } 314 _pool[size] = who; 315 ++size; 316 } 317 else 318 { 319 if (size >= OBJECT_POOL_MAX_SIZE) 320 { 321 cout << capacity << " " << typeid(ObjectPool<ClassType, ThreadSafe>).name() << " warnning Release out-of-size" << endl; 322 delete who; 323 return; 324 } 325 if (size >= capacity) 326 { 327 ReSize(); 328 } 329 _pool[size] = who; 330 ++size; 331 } 332 } 333 /** 334 * @brief delete all objects in this pool 335 */ 336 static void Clear() 337 { 338 if (ThreadSafe) 339 { 340 std::lock_guard<std::mutex> locker(_mutex); 341 auto pool = _pool; 342 auto _size = size; 343 if (_size <= 0)return; 344 for (int i = 0; i < _size; i++) 345 { 346 if (pool[i]) 347 { 348 delete pool[i]; 349 pool[i] = nullptr; 350 } 351 } 352 size = 0; 353 } 354 else 355 { 356 auto pool = _pool; 357 auto _size = size; 358 if (_size <= 0)return; 359 for (int i = 0; i < _size; i++) 360 { 361 if (pool[i]) 362 { 363 delete pool[i]; 364 pool[i] = nullptr; 365 } 366 } 367 size = 0; 368 } 369 } 370 inline static int Size() 371 { 372 if (ThreadSafe) 373 { 374 std::lock_guard<std::mutex> locker(_mutex); 375 return size 376 } 377 else 378 { 379 return size; 380 } 381 } 382 /** 383 * @brief print current pool status for debug 384 */ 385 static void PrintStatus() 386 { 387 int total = (sizeof(ClassType)* size); 388 if (total < 1024) 389 { 390 cout << "capacity:" << capacity << " size:" << size << " memeory:" << (sizeof(ClassType)* size) << "B " << typeid(ObjectPool<ClassType, ThreadSafe>).name() << endl; 391 } 392 else if (total < 1024 * 1024) 393 { 394 cout << "capacity:" << capacity << " size:" << size << " memeory:" << (sizeof(ClassType)* size) / 1024.0 << "KB " << typeid(ObjectPool<ClassType, ThreadSafe>).name() << endl; 395 } 396 else 397 { 398 cout << "capacity:" << capacity << " size:" << size << " memeory:" << (sizeof(ClassType)* size) / 1024.0 / 1024.0 << "MB " << typeid(ObjectPool<ClassType, ThreadSafe>).name() << endl; 399 } 400 } 401}; 402template<class ClassType, bool ThreadSafe> 403ClassType**ObjectPool<ClassType, ThreadSafe>::_pool = new ClassType*[OBJECT_POOL_DEFAULT_ALLOC_SIZE];// 404template<class ClassType, bool ThreadSafe> 405int ObjectPool<ClassType, ThreadSafe>::size = 0; 406template<class ClassType, bool ThreadSafe> 407std::mutex ObjectPool<ClassType, ThreadSafe>::_mutex; 408template<class ClassType, bool ThreadSafe> 409int ObjectPool<ClassType, ThreadSafe>::capacity = OBJECT_POOL_DEFAULT_ALLOC_SIZE; 410 411 412 413 414//自带share_ptr版本 无需手动调用 Object::Release 415//适用于 SharedObjectRecycleable的子类的对象池 416//因为模板的模板 特化 不好处理 因此干脆重新开个名字 ShareObjectPool 417template< typename ClassType, typename bool ThreadSafe = false> 418class ShareObjectPool 419{ 420 /** 421 * @brief inner ctor class for Pool 422 */ 423 template< class ClassType, bool ThreadSafe = false> 424 class Ctor 425 { 426 public: 427 Ctor() 428 {//ctor 429 int capacity = ShareObjectPool<ClassType, ThreadSafe>::capacity; 430 auto pool = ShareObjectPool<ClassType, ThreadSafe>::_pool; 431 for (int i = 0; i < capacity; i++) 432 { 433 pool[i] = nullptr; 434 } 435 ShareObjectPool<ClassType, ThreadSafe>::PreAlloc(); 436 } 437 ~Ctor() 438 {//dtor 439 ShareObjectPool<ClassType, ThreadSafe>::Clear(); 440 //如果编译报错 那么证明不是约束下的 子类关系 请确认 441 ClassType * s = nullptr; 442 s->___type_sub_class_restrain__SharedObjectRecycleable(); 443 } 444 }; 445 ShareObjectPool(); 446 static int capacity; 447 static ClassType** _pool; 448 static int size; 449 static std::mutex _mutex; 450 451private: 452 /** 453 * @brief resize inner pool growing by twice 454 * @warning this will copy old block and delete old 455 */ 456 static void ReSize() 457 { 458 auto pool = new ClassType*[capacity * 2];// 459 memset(pool, 0, sizeof(ClassType*)* 2 * capacity); 460 memcpy(pool, _pool, sizeof(ClassType*)* capacity); 461 capacity *= 2; 462 delete _pool; 463 _pool = pool; 464 } 465 static void PreAlloc() 466 { 467 auto pool = _pool; 468 for (int i = 0; i < OBJECT_POOL_DEFAULT_ALLOC_OBJECT_SIZE; i++) 469 { 470 pool[size] = new ClassType(); 471 ++size; 472 } 473 } 474public: 475 /** 476 * @brief call this to new class who is the sub-class of ShareObjectRecycleable 477 * @note if class size if smaller than OBJECT_POOL_MIN_SIZEOF will new otherwise will gen object from pool 478 */ 479 static share_ptr<ClassType, ThreadSafe> Create() 480 { 481 if (ThreadSafe) 482 { 483 ClassType * ret = nullptr; 484 { 485 std::lock_guard<std::mutex> locker(_mutex); 486 static ShareObjectPool<ClassType, ThreadSafe>::Ctor<ClassType, ThreadSafe> ctor;//c11 compiler suport thread-safe 487 if (size > 0 && sizeof(ClassType) >= OBJECT_POOL_MIN_SIZEOF) 488 { 489 --size; 490 ret = (_pool[size]); 491 _pool[size] = nullptr; 492 } 493 } 494 if (!ret) 495 { 496 ret = new ClassType(); 497 } 498 ret->ctor(); 499 return share_ptr<ClassType, ThreadSafe>(ret); 500 } 501 else 502 { 503 static ShareObjectPool<ClassType, ThreadSafe>::Ctor<ClassType, ThreadSafe> ctor;//c11 compiler suport thread-safe 504 if (size > 0 && sizeof(ClassType) >= OBJECT_POOL_MIN_SIZEOF) 505 { 506 --size; 507 ClassType* ret = (_pool[size]); 508 _pool[size] = nullptr; 509 ret->ctor(); 510 return share_ptr<ClassType, ThreadSafe>(ret); 511 } 512 //ctor will be call in share_ptr::share_ptr(); 513 return share_ptr<ClassType, ThreadSafe>(); 514 } 515 } 516 /** 517 * @brief release object to the pool 518 * @note this will not call manual ShareObjectRecycleable will call this function automatic 519 * @note if sizeof class is smaller than OBJECT_POOL_MIN_SIZEOF will delete now otherwise will put into pool 520 */ 521 static void Release(ClassType* who) 522 { 523 if (!who)return; 524 who->dtor(); 525 if (sizeof(ClassType) < OBJECT_POOL_MIN_SIZEOF) 526 { 527 delete who; 528 return; 529 } 530 if (ThreadSafe) 531 { 532 std::lock_guard<std::mutex> locker(_mutex); 533 if (size >= OBJECT_POOL_MAX_SIZE) 534 { 535 cout << capacity << " " << typeid(ObjectPool<ClassType, ThreadSafe>).name() << " warnning Release out-of-size" << endl; 536 delete who; 537 return; 538 } 539 if (size >= capacity) 540 { 541 ReSize(); 542 } 543 _pool[size] = who; 544 ++size; 545 } 546 else 547 { 548 if (size >= OBJECT_POOL_MAX_SIZE) 549 { 550 cout << capacity << " " << typeid(ObjectPool<ClassType, ThreadSafe>).name() << " warnning Release out-of-size" << endl; 551 delete who; 552 return; 553 } 554 if (size >= capacity) 555 { 556 ReSize(); 557 } 558 _pool[size] = who; 559 ++size; 560 } 561 } 562 /** 563 * @brief delete all objects in this pool 564 */ 565 static void Clear() 566 { 567 if (ThreadSafe) 568 { 569 std::lock_guard<std::mutex> locker(_mutex); 570 if (size <= 0)return; 571 for (int i = 0; i < size; i++) 572 { 573 if (_pool[i]) 574 { 575 delete _pool[i]; 576 _pool[i] = nullptr; 577 } 578 } 579 size = 0; 580 } 581 else 582 { 583 if (size <= 0)return; 584 for (int i = 0; i < size; i++) 585 { 586 if (_pool[i]) 587 { 588 delete _pool[i]; 589 _pool[i] = nullptr; 590 } 591 } 592 size = 0; 593 } 594 } 595 inline static int Size() 596 { 597 if (ThreadSafe) 598 { 599 std::lock_guard<std::mutex> locker(_mutex); 600 return size 601 } 602 else 603 { 604 return size 605 } 606 } 607 /** 608 * @brief print current pool status for debug 609 */ 610 static void PrintStatus() 611 { 612 int total = (sizeof(ClassType)* size); 613 if (total < 1024) 614 { 615 cout << "capacity:" << capacity << " size:" << size << " memeory:" << (sizeof(ClassType)* size) << "B " << typeid(ShareObjectPool<ClassType, ThreadSafe>).name() << endl; 616 } 617 else if (total < 1024 * 1024) 618 { 619 cout << "capacity:" << capacity << " size:" << size << " memeory:" << (sizeof(ClassType)* size) / 1024.0 << "KB " << typeid(ShareObjectPool<ClassType, ThreadSafe>).name() << endl; 620 } 621 else 622 { 623 cout << "capacity:" << capacity << " size:" << size << " memeory:" << (sizeof(ClassType)* size) / 1024.0 / 1024.0 << "MB " << typeid(ShareObjectPool<ClassType, ThreadSafe>).name() << endl; 624 } 625 } 626}; 627template<class ClassType, bool ThreadSafe> 628ClassType**ShareObjectPool<ClassType, ThreadSafe>::_pool = new ClassType*[OBJECT_POOL_DEFAULT_ALLOC_SIZE];// 629template<class ClassType, bool ThreadSafe> 630int ShareObjectPool<ClassType, ThreadSafe>::size = 0; 631template<class ClassType, bool ThreadSafe> 632std::mutex ShareObjectPool<ClassType, ThreadSafe>::_mutex; 633template<class ClassType, bool ThreadSafe> 634int ShareObjectPool<ClassType, ThreadSafe>::capacity = OBJECT_POOL_DEFAULT_ALLOC_SIZE; 635 636 637 638 639//T 只能是 SharedObjectRecycleable的子类 640//12 bytes use in x86 641//20 bytes use in x64 642//ThreadSafe just mean ShareObjectPool not this class(share_ptr) 643//this class is thread safe base-on std::shared_ptr 644template<typename T, bool ThreadSafe = false> 645class share_ptr 646{ 647 struct __inner_ref_wrapper 648 { 649 }; 650public: 651 //请勿随便使用该变量 除非你知道你在干什么 652 T * raw = nullptr;//4 bytes 653 //请勿随便使用该变量 除非你知道你在干什么 654 std::shared_ptr<__inner_ref_wrapper> __ptr = nullptr;//8 bytes 655 ~share_ptr() 656 { 657 //thread-safe 658 if (__ptr.use_count() == 1)//only self ref this 659 { 660 //如果编译报错 那么证明不是约束下的 子类关系 请确认 661 ShareObjectPool<T, ThreadSafe>::Release((raw)); 662 // ptr = NULL; 663 } 664 } 665 inline T *operator->() const 666 { 667 return this->raw; 668 } 669 share_ptr<T, ThreadSafe >& operator = (const share_ptr<T, ThreadSafe> & other) 670 { 671 this->__ptr = other.__ptr; 672 this->raw = other.raw; 673 return *this; 674 } 675 share_ptr() : 676 __ptr(std::make_shared<share_ptr<T, ThreadSafe >::__inner_ref_wrapper>()), raw(new T()) 677 { 678 //new T will call ctor 不在Pool里面调用ctor是为了减少一个share_ptr的 构造 679 raw->ctor(); 680 } 681 682 share_ptr(T*t) : 683 __ptr(std::make_shared<share_ptr<T, ThreadSafe >::__inner_ref_wrapper>()), raw(t) 684 { 685 } 686 share_ptr<T, ThreadSafe >(const share_ptr<T, ThreadSafe > &other) : 687 __ptr(other.__ptr), raw(other.raw) 688 { 689 } 690}; 691/* 692该函数无意义 因为不允许 不和Pool一起使用 693template<typename T, bool ThreadSafe = false> 694inline share_ptr<T, ThreadSafe> make_share() 695{ 696return share_ptr<T, ThreadSafe>(); 697}*/ 698 699 700//ThreadSafe just mean ObjectPool not this class(RawObjectRecycleable) 701template<typename SubClassType, bool ThreadSafe = false> 702class RawObjectRecycleable :public RawObject, public Recycleable 703{ 704public: 705 inline void Release()override 706 { 707 ObjectPool<SubClassType, ThreadSafe>::Release((SubClassType*)this); 708 } 709}; 710//ThreadSafe just mean ObjectPool not this class(RefObjectRecycleable) 711template<typename SubClassType, bool ThreadSafe = false> 712class RefObjectRecycleable :public RefObject, public Recycleable 713{ 714public: 715 inline void Release() override 716 { 717 --referenct_count; 718 if (referenct_count <= 0) 719 { 720 referenct_count = 0; 721 ObjectPool<SubClassType, ThreadSafe>::Release((SubClassType*)this); 722 } 723 } 724}; 725 726//这个 和 其他的不一样 用法比较特别 因为要支持pool 727//创建请用ShareObjectPool<>::Create OR make_share 728template<typename SubClassType> 729class SharedObjectRecycleable : public Recycleable 730{ 731public: 732 //tag for class share_ptr T must is the sub-class of this class 733 //泛型约束 734 inline void ___type_sub_class_restrain__SharedObjectRecycleable() 735 { 736 } 737}; 738 739 740#include<iostream> 741#include "functional" 742#include <chrono> 743#include <thread> 744using namespace std; 745//cpp文件全部都是 测试代码 和 示例代码 746 747class SharedObjectRecycleable111 :public SharedObjectRecycleable<SharedObjectRecycleable111> 748{ 749public: 750 SharedObjectRecycleable111() 751 { 752 cout << "SharedObjectRecycleable111:SharedObjectRecycleable111" << endl; 753 } 754 ~SharedObjectRecycleable111() 755 { 756 cout << "~~SharedObjectRecycleable111:~SharedObjectRecycleable111 " << this << endl; 757 } 758 virtual void ctor() 759 { 760 761 } 762 virtual void dtor() 763 { 764 xx = 10; 765 } 766 void print() 767 { 768 cout << " " << (++xx) << endl; 769 770 } 771 int xx = 10; 772 char sqdfqsd[30];// 773}; 774 775 776class SharedObjectRecycleable222222 :public SharedObjectRecycleable<SharedObjectRecycleable222222> 777{ 778public: 779 SharedObjectRecycleable222222() 780 { 781 // cout << "SharedObjectRecycleable222222:SharedObjectRecycleable222222" << endl; 782 } 783 ~SharedObjectRecycleable222222() 784 { 785 // cout << "~~SharedObjectRecycleable222222:~SharedObjectRecycleable222222 " << this << endl; 786 } 787 virtual void ctor() 788 { 789 790 } 791 virtual void dtor() 792 { 793 xx = 10; 794 } 795 void print() 796 { 797 // cout << " " << (++xx) << endl; 798 799 } 800 int xx = 10; 801 char sqdfqsd[10240000];// 802}; 803 804 805class RefObject2222 :public RefObject 806{ 807public: 808 RefObject2222() 809 { 810 cout << "RefObject2222:RefObject2222" << endl; 811 } 812 ~RefObject2222() 813 { 814 cout << "~~RefObject2222:~RefObject2222 " << this << endl; 815 } 816}; 817 818 819class RefObject1111 :public RefObject 820{ 821public: 822 RefObject1111() 823 { 824 cout << "RefObject1111:RefObject1111" << endl; 825 this->img = new RefObject2222();//默认图片 826 827 } 828 ~RefObject1111() 829 { 830 cout << "~~RefObject1111:~RefObject1111 " << this << endl; 831 if (this->img) 832 { 833 this->img->Release();//计数-1 834 } 835 } 836public: 837 void SetImage(RefObject2222 * img) 838 { 839 if (this->img) 840 { 841 this->img->Release();//计数-1 842 } 843 img->Retain();//计数+1 844 this->img = img; 845 } 846 RefObject2222 * img = nullptr; 847}; 848 849 850 851 852 853class RefObjectRecycleable22222222 :public RefObjectRecycleable<RefObjectRecycleable22222222> 854{ 855public: 856 RefObjectRecycleable22222222() 857 { 858 cout << "RefObjectRecycleable22222222:RefObjectRecycleable22222222" << endl; 859 } 860 ~RefObjectRecycleable22222222() 861 { 862 cout << "~~RefObjectRecycleable22222222:~RefObjectRecycleable22222222 " << this << endl; 863 } 864 void ctor() {}; 865 void dtor(){}; 866 char sqdfqsd[1024];// 867}; 868 869 870class RefObjectRecycleable1111111111 :public RefObjectRecycleable<RefObjectRecycleable1111111111> 871{ 872public: 873 void ctor() {}; 874 void dtor(){}; 875 RefObjectRecycleable1111111111() 876 { 877 cout << "RefObjectRecycleable1111111111" << endl; 878 this->img = ObjectPool<RefObjectRecycleable22222222>::Create();//默认图片 879 880 } 881 ~RefObjectRecycleable1111111111() 882 { 883 cout << "~~RefObjectRecycleable1111111111 " << this << endl; 884 if (this->img) 885 { 886 this->img->Release();//计数-1 887 } 888 } 889 char sqdfqsd[1024];// 890public: 891 void SetImage(RefObjectRecycleable22222222 * img) 892 { 893 if (this->img) 894 { 895 this->img->Release();//计数-1 896 } 897 img->Retain();//计数+1 898 this->img = img; 899 } 900 RefObjectRecycleable22222222 * img = nullptr; 901}; 902 903 904 905 906 907class SharedObject111 :public SharedObject<SharedObject111> 908{ 909public: 910 SharedObject111() 911 { 912 cout << "SharedObject111:SharedObject111" << endl; 913 } 914 ~SharedObject111() 915 { 916 cout << "~~SharedObject111:~SharedObject111 " << this << endl; 917 } 918}; 919 920class RawObjectRecycleable11111111 : public RawObjectRecycleable<RawObjectRecycleable11111111> 921{ 922public: 923 RawObjectRecycleable11111111() 924 { 925 cout << "RawObjectRecycleable11111111" << endl; 926 } 927 ~RawObjectRecycleable11111111() 928 { 929 cout << "~~RawObjectRecycleable11111111 " << this << endl; 930 } 931 int x = 5; 932 void print() 933 { 934 cout << "print" << endl; 935 } 936 void ctor() {}; 937 void dtor(){}; 938 939 char sss[1024]; 940 941}; 942 943class RawObjectRecycleable666666 : public RawObjectRecycleable<RawObjectRecycleable666666, true> 944{ 945public: 946 RawObjectRecycleable666666() 947 { 948 // cout << "RawObjectRecycleable666666" << endl; 949 } 950 ~RawObjectRecycleable666666() 951 { 952 //cout << "~~RawObjectRecycleable666666 " << this << endl; 953 } 954 int x = 5; 955 void print() 956 { 957 cout << "print " << (++x) << endl; 958 } 959 void ctor() {}; 960 void dtor(){ x = 5; }; 961 962 char sss[10240]; 963 964}; 965 966 967 968#if _WIN32 969#include "windows.h" 970#include <WinBase.h> 971int calculateMS(std::function<void()> processFunc) 972{ 973 long long _value; 974 LARGE_INTEGER freq, _start, _end; 975 QueryPerformanceFrequency(&freq); 976 QueryPerformanceCounter(&_start); 977 978 processFunc(); 979 980 QueryPerformanceCounter(&_end); 981 _value = (_end.QuadPart - _start.QuadPart) * 1000 / freq.QuadPart; 982 983 return _value; 984} 985 986 987 988int calculateUS(std::function<void()> processFunc) 989{ 990 long long _value; 991 LARGE_INTEGER freq, _start, _end; 992 QueryPerformanceFrequency(&freq); 993 QueryPerformanceCounter(&_start); 994 995 processFunc(); 996 997 QueryPerformanceCounter(&_end); 998 _value = (_end.QuadPart - _start.QuadPart) * 1000 * 1000 / freq.QuadPart; 999 1000 return _value; 1001} 1002 1003 1004 1005 1006 1007void func_test_benchmark_share_pool() 1008{ 1009 printf("%d \r\n\n", calculateUS([=]() 1010 { 1011 for (int i = 0; i != 10000; i++) 1012 { 1013 auto s1 = 1014 ShareObjectPool< SharedObjectRecycleable111>::Create(); 1015 auto s2 = 1016 ShareObjectPool< SharedObjectRecycleable111>::Create(); 1017 auto s3 = 1018 ShareObjectPool< SharedObjectRecycleable111>::Create(); 1019 auto s4 = 1020 ShareObjectPool< SharedObjectRecycleable111>::Create(); 1021 1022 1023 } 1024 })); 1025 1026 printf("%d \r\n\n", calculateUS([=]() 1027 { 1028 for (int i = 0; i != 10000; i++) 1029 { 1030 auto s1 = make_shared<SharedObjectRecycleable111>(); 1031 auto s2 = make_shared<SharedObjectRecycleable111>(); 1032 auto s3 = make_shared<SharedObjectRecycleable111>(); 1033 auto s4 = make_shared<SharedObjectRecycleable111>(); 1034 1035 1036 } 1037 })); 1038 printf("%d \r\n\n", calculateUS([=]() 1039 { 1040 for (int i = 0; i != 10000; i++) 1041 { 1042 auto s1 = new SharedObjectRecycleable111(); 1043 delete s1; 1044 auto s2 = new SharedObjectRecycleable111(); 1045 delete s2; 1046 auto s3 = new SharedObjectRecycleable111(); 1047 delete s3; 1048 auto s4 = new SharedObjectRecycleable111(); 1049 delete s4; 1050 1051 } 1052 })); 1053 auto s11 = 1054 ShareObjectPool< SharedObjectRecycleable111>::Create(); 1055 1056 1057 ShareObjectPool<SharedObjectRecycleable111>::Clear(); 1058 ShareObjectPool< SharedObjectRecycleable111>::Clear(); 1059} 1060 1061#endif 1062 1063//传递方法和 std::shared_ptr 一样 1064void func_test_share_pool_2(share_ptr<SharedObjectRecycleable111, false> obj) 1065{ 1066 //添加了一个引用 1067 //引用为2 1068 obj->print(); 1069 cout << obj.raw << endl; 1070} 1071//测试 ShareObjectPool 和example 1072void func_test_share_pool() 1073{ 1074 { 1075 { 1076 auto obj1 = ShareObjectPool< SharedObjectRecycleable111>::Create(); 1077 1078 auto obj2 = obj1;//一个拷贝 和std::shared_ptr一样 直接传递对象即可 1079 obj1->print(); 1080 cout << obj1.raw << endl; 1081 } 1082 //执行到这里 没有任何引用了 已经被 回收 Pool大小为1 1083 1084 1085 //obj1 是刚刚回收的对象 和上面的是同一个对象 1086 auto obj1 = ShareObjectPool< SharedObjectRecycleable111>::Create(); 1087 obj1->print(); 1088 cout << obj1.raw << endl; 1089 1090 //pool大小为0 创建了一个新的对象 1091 auto obj2 = ShareObjectPool< SharedObjectRecycleable111>::Create(); 1092 obj2->print();//引用为1 1093 cout << obj2.raw << endl; 1094 1095 func_test_share_pool_2(obj2);//执行完成后引用为1了 1096 //由于pool为空 所以无效 1097 ShareObjectPool< SharedObjectRecycleable111>::Clear(); 1098 cout << "-------" << endl; 1099 } 1100 //大小为2 被delete 1101 ShareObjectPool< SharedObjectRecycleable111>::Clear(); 1102} 1103 1104void func_test_ref_object() 1105{ 1106 { 1107 RefObject1111 *obj = new RefObject1111;//计数1 1108 obj->Retain();//计数2 1109 1110 obj->Release();//计数1 1111 obj->Release();//计数0 被delete 1112 1113 1114 1115 RefObject1111 *obj1 = new RefObject1111;//计数1 1116 obj1->Release();//计数0 被delete 1117 1118 1119 RefObject1111 *obj2 = new RefObject1111;//计数1 1120 //obj2 泄露了 1121 } 1122 1123 cout << "............0" << endl; 1124 { 1125 RefObject2222 *img = new RefObject2222;//计数1 图片资源 1126 1127 RefObject1111*sprite = new RefObject1111;//计数1 图片拥有者 1128 1129 sprite->SetImage(img);//--img计数2 1130 img->Release();//变量img的 作用域交出控制权 img计数1 1131 sprite->Release();//变量sprite 交出控制权 但是无其他引用 都被delete掉了 1132 //img sprite 被全部清理了 1133 } 1134} 1135 1136 1137void func_test_share_object() 1138{ 1139 auto obj1 = std::make_shared<SharedObject111>();//ok create one 1140 1141 std::shared_ptr<SharedObject111> obj0;// none ref 1142 1143 { 1144 auto obj1 = std::make_shared<SharedObject111>();//ok create one 1145 { 1146 auto obj2 = obj1->shared_from_this();//create new from shared 1147 obj0 = obj2->shared_from_this();//create from obj2 1148 auto obj3 = obj1;//create from obj2 1149 1150 } 1151 } 1152 1153 { 1154 auto obj11 = std::make_shared<SharedObject111>();//ok create one 1155 } 1156 //obj11 has been delete 1157 cout << "1111" << endl; 1158} 1159 1160void func_test_raw_object_pool() 1161{ 1162 auto obj1 = ObjectPool<RawObjectRecycleable11111111>::Create(); 1163 obj1->Release(); 1164 1165 1166 auto obj2 = ObjectPool<RawObjectRecycleable11111111>::Create(); 1167 obj2->Release(); 1168 1169 auto obj3 = ObjectPool<RawObjectRecycleable11111111>::Create(); 1170 obj3->Release(); 1171 1172 cout << "111111111111" << endl; 1173 ObjectPool<RawObjectRecycleable11111111, false>::Clear(); 1174 1175} 1176 1177void func_test_ref_object_pool() 1178{ 1179 1180 RefObjectRecycleable22222222 *obj = ObjectPool<RefObjectRecycleable22222222>::Create();//计数1 1181 cout << " obj " << obj << endl; 1182 obj->Retain();//计数2 1183 1184 obj->Release();//计数1 1185 obj->Release();//计数0 被 ObjectPool回收 1186 1187 RefObjectRecycleable22222222 *obj1 = ObjectPool<RefObjectRecycleable22222222>::Create();//计数1 1188 cout << " obj1 " << obj1 << endl; 1189 obj1->Release();//计数0 被 ObjectPool回收 1190 1191 1192 cout << "............0" << endl; 1193 1194 RefObjectRecycleable1111111111 *obj2 = ObjectPool<RefObjectRecycleable1111111111>::Create();//计数1 1195 cout << " obj2 " << obj2 << endl; 1196 cout << " obj2 img " << obj2->img << endl;//this img is in old object obj 1197 1198 1199 1200 obj2->Release();//计数0 被 ObjectPool回收 1201 1202 1203 1204 1205 /* RefObject1111 *obj1 = new RefObject1111;//计数1 1206 obj1->Release();//计数0 被delete 1207 1208 1209 RefObject1111 *obj2 = new RefObject1111;//计数1 1210 //obj2 泄露了 1211 } 1212 1213 cout << "............0" << endl; 1214 { 1215 RefObject2222 *img = new RefObject2222;//计数1 图片资源 1216 1217 RefObject1111*sprite = new RefObject1111;//计数1 图片拥有者 1218 1219 sprite->SetImage(img);//--img计数2 1220 img->Release();//变量img的 作用域交出控制权 img计数1 1221 sprite->Release();//变量sprite 交出控制权 但是无其他引用 都被delete掉了 1222 //img sprite 被全部清理了 1223 1224 1225 */ 1226 1227 cout << "............0" << endl; 1228 ObjectPool<RefObjectRecycleable22222222>::Clear(); 1229} 1230 1231 1232void func_test_share_object_pool_thread_safe() 1233{ 1234 for (int i = 0; i < 10; i++) 1235 { 1236 std::thread t([=] 1237 { 1238 int i = 300; 1239 1240 while (--i>0) 1241 { 1242 1243 1244 auto s4 = 1245 ShareObjectPool< SharedObjectRecycleable222222, true>::Create(); 1246 1247 // auto obj1 = ObjectPool<RawObjectRecycleable666666, true>::Create(); 1248 // cout << obj1 << endl; 1249 // if (obj1) obj1->Release(); 1250 // ObjectPool<RawObjectRecycleable666666, true>::Clear(); 1251 } 1252 cout << "............thread exit" << endl; 1253 }); 1254 t.detach(); 1255 } 1256 this_thread::sleep_for(std::chrono::milliseconds(20)); 1257 1258 ShareObjectPool< SharedObjectRecycleable222222, true>::PrintStatus(); 1259 1260 ShareObjectPool< SharedObjectRecycleable222222, true>::Clear(); 1261} 1262 1263void func_test_raw_object_pool_thread_safe() 1264{ 1265 for (int i = 0; i < 10; i++) 1266 { 1267 std::thread t([=] 1268 { 1269 int i = 3000; 1270 1271 while (--i>0) 1272 { 1273 auto obj1 = ObjectPool<RawObjectRecycleable666666, true>::Create(); 1274 obj1->Release(); 1275 1276 1277 auto obj2 = ObjectPool<RawObjectRecycleable666666, true>::Create(); 1278 obj2->Release(); 1279 1280 1281 } 1282 cout << "............thread exit" << endl; 1283 }); 1284 t.detach(); 1285 } 1286 1287 this_thread::sleep_for(std::chrono::milliseconds(20)); 1288 1289 auto obj1 = ObjectPool<RawObjectRecycleable666666, true>::Create(); 1290 obj1->Release(); 1291 1292 1293 ObjectPool< RawObjectRecycleable666666, true>::PrintStatus(); 1294 1295 ObjectPool< RawObjectRecycleable666666, true>::Clear(); 1296 1297 1298} 1299 1300static std::mutex __mutex; 1301void __func_mutex() 1302{ 1303 // std::lock_guard<std::mutex> loc(__mutex); 1304 __mutex.lock(); 1305 static int xx = 0; 1306 xx++; 1307 __mutex.unlock(); 1308} 1309 1310void func_test_raw_object_pool_thread_safe__bug_() 1311{ 1312 for (int i = 0; i < 10; i++) 1313 { 1314 std::thread t([=] 1315 { 1316 int i = 30000; 1317 while (i>0) 1318 { 1319 // std::this_thread::yield(); 1320 //Sleep(1);// will reduce bug rate 1321 __func_mutex();//BUG when main function return static variable _mutex was destroy but sub-thread still use this. 1322 //mutex destroyed while busy 1323 //TODO fixed me 1324 } 1325 cout << "............thread exit" << endl; 1326 }); 1327 t.detach(); 1328 } 1329 this_thread::sleep_for(std::chrono::milliseconds(2000)); 1330 1331} 1332 1333int main(int argc, char* argv[]) 1334{ 1335 func_test_raw_object_pool_thread_safe__bug_(); 1336 1337 // func_test_raw_object_pool_thread_safe(); 1338 //Sleep(10000000);// 20 second 1339 //exit(0); 1340 system("pause"); 1341 1342 1343 return 0; 1344}
点赞
收藏

评论区

加载中...

相关推荐

MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1

文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s

Oracle 分组与拼接字符串同时使用

SELECTT.,ROWNUMIDFROM(SELECTT.EMPLID,T.NAME,T.BU,T.REALDEPART,T.FORMATDATE,SUM(T.S0)S0,MAX(UPDATETIME)CREATETIME,LISTAGG(TOCHAR(

MySQL部分从库上面因为大量的临时表tmp_table造成慢查询

背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_

皕杰报表之UUID

​在我们用皕杰报表工具设计填报报表时,如何在新增行里自动增加id呢?能新增整数排序id吗?目前可以在新增行里自动增加id,但只能用uuid函数增加UUID编码,不能新增整数排序id。uuid函数说明:获取一个UUID,可以在填报表中用来创建数据ID语法:uuid()或uuid(sep)参数说明:sep布尔值,生成的uuid中是否包含分隔符'',缺省为

手写Java HashMap源码

HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22

2020年前端实用代码段,为你的工作保驾护航

有空的时候,自己总结了几个代码段,在开发中也经常使用,谢谢。1、使用解构获取json数据let jsonData  id: 1,status: "OK",data: 'a', 'b';let  id, status, data: number   jsonData;console.log(id, status, number )