dl
dl
base
dl_list_mt_cache.h
浏览该文件的文档.
1
14
#pragma once
15
16
#include <list>
17
18
#include "
base/dl_type_def.h
"
19
20
namespace
dl
21
{
23
template
<
typename
T>
24
class
ListMultiThreadCache
25
{
26
public
:
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
117
void
SetQueueMode
(
QueueMode
mode)
118
{
119
_mode = mode;
120
}
121
122
ListMultiThreadCache
(
QueueMode
mode =
QueueMode::FIFO
) :
123
_mode{ mode }
124
{}
125
private
:
126
std::mutex _mutex;
// 队列锁
127
std::list<T> _list;
// 先入先出队列
128
std::list<T> _listTemp;
// 主线程缓冲
129
QueueMode
_mode;
130
};
131
}
dl::ListMultiThreadCache::Move
void Move()
结束添加时调用,或立即刷新
定义
dl_list_mt_cache.h:79
dl::ListMultiThreadCache::PopFront
bool PopFront(T &a)
取第一个,顺序通过 SetQueueMode 指定,默认FIFO
定义
dl_list_mt_cache.h:32
dl::ListMultiThreadCache::Add
void Add(T &&buf)
添加
定义
dl_list_mt_cache.h:64
dl::ListMultiThreadCache::SetQueueMode
void SetQueueMode(QueueMode mode)
定义
dl_list_mt_cache.h:117
dl::ListMultiThreadCache::GetSize
size_t GetSize()
添加
定义
dl_list_mt_cache.h:91
dl::ListMultiThreadCache::IsEmpty
bool IsEmpty()
所有任务完成
定义
dl_list_mt_cache.h:100
dl::ListMultiThreadCache::Clear
void Clear()
添加
定义
dl_list_mt_cache.h:109
dl::ListMultiThreadCache::Add
void Add(const T &buf)
添加
定义
dl_list_mt_cache.h:49
dl::ListMultiThreadCache::ListMultiThreadCache
ListMultiThreadCache(QueueMode mode=QueueMode::FIFO)
定义
dl_list_mt_cache.h:122
dl_type_def.h
通用类型定义
dl
定义
dl_array.h:22
dl::KeyCode::T
@ T
定义
dl_input.h:121
dl::QueueMode
QueueMode
定义
dl_type_def.h:106
dl::QueueMode::FIFO
@ FIFO
定义
dl_type_def.h:107
制作者
1.13.1