#ifndef HGL_THREAD_SWAP_LIST_INCLUDE #define HGL_THREAD_SWAP_LIST_INCLUDE #include #include #include #include namespace hgl { /** * 数据交换合集 */ template class S> class SwapColl { protected: S join_list; S proc_list; ThreadMutex lock; public: virtual ~SwapColl(){} /** * 加入一个数据到合集中 */ bool Add(T &item) { lock.Lock(); int result=join_list.Add(item); lock.Unlock(); return(result!=-1); } /** * 增加一批数据到合集中 */ int Add(T *item_list,const int count) { lock.Lock(); int result=join_list.Add(item_list,count); lock.Unlock(); return result; } /** * 取得可以使用的列表 */ S &GetProcList() { lock.Lock(); proc_list.Add(join_list); join_list.Clear(); lock.Unlock(); return proc_list; } S *operator ->(){return &proc_list;} operator S &(){return proc_list;} };//template class SwapColl template using SwapList=SwapColl; ///<安全交换列表 template using SwapSet=SwapColl; ///<安全交换集合 /** * 信号安全交换合集 */ template class S> class SemSwapColl { protected: S join_list; S proc_list; ThreadMutex lock; Semaphore sem; public: virtual ~SemSwapColl()=default; /** * 增加一个数据到合集中 */ bool Add(T &item) { lock.Lock(); int result=join_list.Add(item); lock.Unlock(); if(result!=-1) sem.Post(1); return(result!=-1); } /** * 增加一批数据到合集中 */ int Add(T *item_list,const int count) { lock.Lock(); int result=join_list.Add(item_list,count); lock.Unlock(); if(result>0) sem.Post(1); return result; } /** * 等待信号 */ bool WaitProc(const double time_out=HGL_TIME_ONE_MINUTE) { if(!sem.Acquire(time_out)) return(false); lock.Lock(); proc_list.Add(join_list); join_list.Clear(); lock.Unlock(); return(true); } /** * 等待信号 */ bool TryProc() { if(!sem.TryAcquire()) return(false); lock.Lock(); proc_list.Add(join_list); join_list.Clear(); lock.Unlock(); return(true); } S *operator ->(){return &proc_list;} operator S &(){return proc_list;} };//template class SemSwapColl template using SemSwapList=SemSwapColl; ///<安全信号交换列表 template using SemSwapSet=SemSwapColl; ///<安全信号交换集合 }//namespace hgl #endif//HGL_THREAD_SWAP_LIST_INCLUDE