main.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. #include <unordered_map>
  2. #include <type_traits>
  3. #include <typeinfo>
  4. #include <iostream>
  5. #include "log_file.h"
  6. class ObjectPoolLogPoint
  7. {
  8. public:
  9. ObjectPoolLogPoint() :
  10. count(0)
  11. {
  12. }
  13. int count;
  14. };
  15. template<typename T>
  16. class ObjectPool {
  17. public:
  18. static ObjectPool<T> default_object_pool;
  19. static T* CreatePoolObject() {
  20. return default_object_pool.allocate();
  21. }
  22. static void DestroyPoolObject(T* p)
  23. {
  24. return default_object_pool.deallocate(p);
  25. }
  26. explicit ObjectPool(const char* name):name_(name) {
  27. }
  28. ~ObjectPool()
  29. {
  30. _ASSERT(all_cnt_ == 0);
  31. for(auto item : objects_) {
  32. delete item;
  33. }
  34. }
  35. ObjectPool(ObjectPool&) = delete;
  36. ObjectPool& operator=(ObjectPool&) = delete;
  37. T* allocate() {
  38. all_cnt_++;
  39. if(objects_.size() > 0) {
  40. T* t = static_cast<T*>(*objects_.begin());
  41. objects_.pop_front();
  42. return t;
  43. }
  44. return new T;
  45. }
  46. void deallocate(T* pData)
  47. {
  48. all_cnt_--;
  49. objects_.push_back(pData);
  50. }
  51. private:
  52. int all_cnt_{};
  53. const char* name_{};
  54. std::list<T*> objects_;
  55. //std::unordered_map <std::string, ObjectPoolLogPoint> logPoints_;
  56. };
  57. template<typename T> ObjectPool<T> ObjectPool<T>::default_object_pool(typeid(T).name());
  58. class pack1
  59. {
  60. public:
  61. virtual ~pack1() = default;
  62. };
  63. class Packet :public pack1
  64. {
  65. public:
  66. void onReclaimObject() {
  67. a = 0;
  68. b = 0;
  69. }
  70. int a{};
  71. int b{};
  72. };
  73. template<typename T, typename... Args>
  74. static inline T* new_object1(Args&& ... args)
  75. {
  76. return ObjectPool<T>::CreatePoolObject(std::forward<Args>(args)...);
  77. }
  78. template<typename T>
  79. static inline void delete_object(T* ptr)
  80. {
  81. ObjectPool<T>::DestroyPoolObject(ptr);
  82. }
  83. void func(TTripleBuffer<std::vector<int>>& triple_buffer, const bool& flag)
  84. {
  85. thread_local int i = -1;
  86. while(true)
  87. {
  88. std::this_thread::sleep_for(std::chrono::milliseconds(20));
  89. triple_buffer.SwapReadBuffers();
  90. auto& read_buffer = triple_buffer.Read();
  91. for(const auto& item : read_buffer)
  92. {
  93. std::cout << item << std::endl;
  94. if(++i != item)
  95. {
  96. _ASSERT(false);
  97. }
  98. i = item;
  99. }
  100. read_buffer.clear();
  101. if(!flag && !triple_buffer.IsDirty())
  102. {
  103. break;
  104. }
  105. }
  106. }
  107. void func2(bool& flag)
  108. {
  109. while(true)
  110. {
  111. if(flag)
  112. {
  113. std::cout << flag << std::endl;
  114. MemoryBarrier();
  115. flag = false;
  116. }
  117. }
  118. }
  119. int main()
  120. {
  121. bool flag = true;
  122. TTripleBuffer<std::vector<int>> triple_buffer;
  123. std::thread t1(func, std::ref(triple_buffer), std::cref(flag));
  124. for(int i = 0; i < 10000; ++i)
  125. {
  126. triple_buffer.GetWriteBuffer().push_back(i);
  127. triple_buffer.SwapWriteBuffers();
  128. std::this_thread::sleep_for(std::chrono::milliseconds(10));
  129. }
  130. while(triple_buffer.IsDirty())
  131. {
  132. std::this_thread::sleep_for(std::chrono::milliseconds(100));
  133. }
  134. triple_buffer.SwapWriteBuffers();
  135. flag = false;
  136. t1.join();
  137. return 0;
  138. }