dl
dl_list_mt_cache.h
浏览该文件的文档.
1
14#pragma once
15
16#include <list>
17
18#include "base/dl_type_def.h"
19
20namespace dl
21{
23template<typename T>
25{
26public:
32 bool PopFront(T& a)
33 {
34 _mutex.lock();
35 if (_list.empty())
36 {
37 _mutex.unlock();
38 return false;
39 }
40 a = _list.front();
41 _list.pop_front();
42 _mutex.unlock();
43 return true;
44 }
45
49 void Add(const T& buf)
50 {
51 if (_mutex.try_lock())
52 {
53 _list.splice(_list.begin(), _listTemp);
54 _list.push_back(buf);
55 _mutex.unlock();
56 return;
57 }
58 _listTemp.push_back(buf);
59 }
60
64 void Add(T&& buf)
65 {
66 if (_mutex.try_lock())
67 {
68 _list.splice(_list.begin(), _listTemp);
69 _list.push_back(buf);
70 _mutex.unlock();
71 return;
72 }
73 _listTemp.push_back(buf);
74 }
75
79 void Move()
80 {
81 if (_listTemp.empty())
82 return;
83 _mutex.lock();
84 _list.splice(_list.begin(), _listTemp);
85 _mutex.unlock();
86 }
87
91 size_t GetSize()
92 {
93 std::lock_guard guard{ _mutex };
94 return _list.size() + _listTemp.size();
95 }
96
100 bool IsEmpty()
101 {
102 std::lock_guard guard{ _mutex };
103 return _list.empty() && _listTemp.empty();
104 }
105
109 void Clear()
110 {
111 _mutex.lock();
112 _list.clear();
113 _mutex.unlock();
114 _listTemp.clear();
115 }
116
118 {
119 _mode = mode;
120 }
121
123 _mode{ mode }
124 {}
125private:
126 std::mutex _mutex;// 队列锁
127 std::list<T> _list;// 先入先出队列
128 std::list<T> _listTemp;// 主线程缓冲
129 QueueMode _mode;
130};
131}
void Move()
结束添加时调用,或立即刷新
bool PopFront(T &a)
取第一个,顺序通过 SetQueueMode 指定,默认FIFO
void SetQueueMode(QueueMode mode)
bool IsEmpty()
所有任务完成
void Add(const T &buf)
添加
ListMultiThreadCache(QueueMode mode=QueueMode::FIFO)
通用类型定义