Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
cosmicing
Prj-Cpps
提交
f6a1c621
P
Prj-Cpps
项目概览
cosmicing
/
Prj-Cpps
通知
2
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
Prj-Cpps
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
f6a1c621
编写于
10月 31, 2022
作者:
cosmicing
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
更新00-ProgramElement/1-NolockQueue/include/lock_free_queue.hpp
上级
05c113f2
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
131 addition
and
0 deletion
+131
-0
00-ProgramElement/1-NolockQueue/include/lock_free_queue.hpp
00-ProgramElement/1-NolockQueue/include/lock_free_queue.hpp
+131
-0
未找到文件。
00-ProgramElement/1-NolockQueue/include/lock_free_queue.hpp
0 → 100644
浏览文件 @
f6a1c621
/*
* Copyright (c) TD-Tech. 2022. All rights reserved.
* Description: lock free queuea
* Author: ousixin
*/
#pragma once
#include <atomic>
#include <limits>
#include <thread>
namespace
add
{
template
<
typename
T
,
size_t
cap
>
class
LockFreeQueue
final
{
static_assert
(
cap
>
0u
,
"capacity of LockFreeQueue should greater than 0"
);
// a redundant location to determine if the queue is empty or full
static
constexpr
size_t
nMax
=
cap
+
1u
;
public:
LockFreeQueue
()
{}
~
LockFreeQueue
()
{}
bool
push
(
const
T
&
elem
)
{
size_t
wIdx
=
writeIdx_
.
load
();
size_t
updateIdx
;
size_t
woff
;
do
{
if
(
wIdx
>
std
::
numeric_limits
<
size_t
>::
max
()
-
1u
)
{
updateIdx
=
((
wIdx
%
nMax
)
+
1u
)
%
nMax
;
}
else
{
updateIdx
=
wIdx
+
1u
;
}
// offset to write
woff
=
wIdx
%
nMax
;
// full
if
(
woff
==
headIdx_
.
load
()
%
nMax
)
{
return
false
;
}
}
while
(
!
writeIdx_
.
compare_exchange_strong
(
wIdx
,
updateIdx
));
datas_
[
woff
]
=
elem
;
while
(
true
)
{
size_t
expect
=
wIdx
;
if
(
!
tailIdx_
.
compare_exchange_weak
(
expect
,
updateIdx
))
{
std
::
this_thread
::
yield
();
}
else
{
break
;
}
}
count_
.
fetch_add
(
1
);
return
true
;
}
bool
push
(
T
&&
elem
)
{
size_t
wIdx
=
writeIdx_
.
load
();
size_t
updateIdx
;
size_t
woff
;
do
{
if
(
wIdx
>
std
::
numeric_limits
<
size_t
>::
max
()
-
1u
)
{
updateIdx
=
((
wIdx
%
nMax
)
+
1u
)
%
nMax
;
}
else
{
updateIdx
=
wIdx
+
1u
;
}
// offset to write
woff
=
wIdx
%
nMax
;
// full
if
(
woff
==
headIdx_
.
load
()
%
nMax
)
{
return
false
;
}
}
while
(
!
writeIdx_
.
compare_exchange_strong
(
wIdx
,
updateIdx
));
datas_
[
woff
]
=
std
::
move
(
elem
);
while
(
true
)
{
size_t
expect
=
wIdx
;
if
(
!
tailIdx_
.
compare_exchange_weak
(
expect
,
updateIdx
))
{
std
::
this_thread
::
yield
();
}
else
{
break
;
}
}
count_
.
fetch_add
(
1
);
return
true
;
}
bool
pop
(
T
&
elem
)
{
size_t
rBeginIdx
=
headIdx_
.
load
();
size_t
updateIdx
;
size_t
roff
;
do
{
if
(
rBeginIdx
>
std
::
numeric_limits
<
size_t
>::
max
()
-
1u
)
{
updateIdx
=
((
rBeginIdx
%
nMax
)
+
1u
)
%
nMax
;
}
else
{
updateIdx
=
rBeginIdx
+
1u
;
}
// offset to read, the headIdx_ is one ahead, read the idx behind the the headIdx_
roff
=
updateIdx
%
nMax
;
// empty
if
(
roff
==
tailIdx_
.
load
()
%
nMax
)
{
return
false
;
}
elem
=
datas_
[
roff
];
// don't move, as CAS may failed
}
while
(
!
headIdx_
.
compare_exchange_strong
(
rBeginIdx
,
updateIdx
));
count_
.
fetch_sub
(
1
);
return
true
;
}
size_t
capacity
()
const
{
return
cap
;
}
size_t
size
()
const
{
return
count_
.
load
();
}
bool
empty
()
const
{
return
count_
.
load
()
==
0u
;
}
bool
full
()
const
{
return
count_
.
load
()
==
cap
;
}
private:
LockFreeQueue
(
const
LockFreeQueue
&
)
=
delete
;
LockFreeQueue
&
operator
=
(
const
LockFreeQueue
&
)
=
delete
;
private:
T
datas_
[
nMax
];
std
::
atomic_size_t
headIdx_
{
nMax
-
1
};
std
::
atomic_size_t
tailIdx_
{
0
};
std
::
atomic_size_t
writeIdx_
{
0
};
std
::
atomic_size_t
count_
{
0
};
};
}
// namespace add
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录