123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169 |
- #include <unordered_map>
- #include <type_traits>
- #include <typeinfo>
- #include <iostream>
- #include "log_file.h"
- class ObjectPoolLogPoint
- {
- public:
- ObjectPoolLogPoint() :
- count(0)
- {
- }
- int count;
- };
- template<typename T>
- class ObjectPool {
-
- public:
- static ObjectPool<T> default_object_pool;
- static T* CreatePoolObject() {
- return default_object_pool.allocate();
- }
- static void DestroyPoolObject(T* p)
- {
- return default_object_pool.deallocate(p);
- }
- explicit ObjectPool(const char* name):name_(name) {
-
- }
- ~ObjectPool()
- {
- _ASSERT(all_cnt_ == 0);
- for(auto item : objects_) {
- delete item;
- }
- }
- ObjectPool(ObjectPool&) = delete;
- ObjectPool& operator=(ObjectPool&) = delete;
- T* allocate() {
- all_cnt_++;
- if(objects_.size() > 0) {
- T* t = static_cast<T*>(*objects_.begin());
- objects_.pop_front();
- return t;
- }
- return new T;
- }
- void deallocate(T* pData)
- {
- all_cnt_--;
- objects_.push_back(pData);
- }
- private:
- int all_cnt_{};
- const char* name_{};
- std::list<T*> objects_;
- //std::unordered_map <std::string, ObjectPoolLogPoint> logPoints_;
- };
- template<typename T> ObjectPool<T> ObjectPool<T>::default_object_pool(typeid(T).name());
- class pack1
- {
- public:
- virtual ~pack1() = default;
- };
- class Packet :public pack1
- {
- public:
-
- void onReclaimObject() {
- a = 0;
- b = 0;
- }
- int a{};
- int b{};
- };
- template<typename T, typename... Args>
- static inline T* new_object1(Args&& ... args)
- {
- return ObjectPool<T>::CreatePoolObject(std::forward<Args>(args)...);
- }
- template<typename T>
- static inline void delete_object(T* ptr)
- {
- ObjectPool<T>::DestroyPoolObject(ptr);
- }
- void func(TTripleBuffer<std::vector<int>>& triple_buffer, const bool& flag)
- {
- thread_local int i = -1;
- while(true)
- {
- std::this_thread::sleep_for(std::chrono::milliseconds(20));
- triple_buffer.SwapReadBuffers();
- auto& read_buffer = triple_buffer.Read();
- for(const auto& item : read_buffer)
- {
- std::cout << item << std::endl;
- if(++i != item)
- {
- _ASSERT(false);
- }
- i = item;
- }
- read_buffer.clear();
- if(!flag && !triple_buffer.IsDirty())
- {
- break;
- }
- }
- }
- void func2(bool& flag)
- {
- while(true)
- {
- if(flag)
- {
- std::cout << flag << std::endl;
- MemoryBarrier();
- flag = false;
- }
- }
- }
- int main()
- {
- bool flag = true;
- TTripleBuffer<std::vector<int>> triple_buffer;
- std::thread t1(func, std::ref(triple_buffer), std::cref(flag));
- for(int i = 0; i < 10000; ++i)
- {
- triple_buffer.GetWriteBuffer().push_back(i);
- triple_buffer.SwapWriteBuffers();
- std::this_thread::sleep_for(std::chrono::milliseconds(10));
- }
- while(triple_buffer.IsDirty())
- {
- std::this_thread::sleep_for(std::chrono::milliseconds(100));
- }
- triple_buffer.SwapWriteBuffers();
- flag = false;
- t1.join();
- return 0;
- }
|