admin管理员组文章数量:1431413
I want to understand CAS logic, and write my own stack code without pop function. I doubt that my function is whether vulnerable or not.
#define MAX_STACK_ENTRY 256
int stack_entry[MAX_STACK_ENTRY];
int entry_num = 0;
void push(int inp)
{
int last;
__atomic_load(&entry_num, &last, __ATOMIC_RELAXED);
do
{
// because we want to check stack is full, first load entry_num
if(last >= MAX_STACK_ENTRY)
{
printf("stack is full");
return;
}
// check entry_num == last -> this thread success CAS. -> entry_num = last + 1
// check entry_num != last -> another thread success CAS. -> last = entry_num
} while(!__atomic_compare_exchange_n(&entry_num, &last, last+1, 1, __ATOMIC_RELAXED, __ATOMIC_RELAXED));
stack_entry[last] = inp;
}
I think this code is not vulnerable unless there is no pop function. __atomic_compare_exchange_n
doesn't need to get other memory order. ot.
Preventing reordering is not needed, because operations are dependent with each other and executed sequentially.
I think it is not correct, then,
while(!__atomic_compare_exchange_n(&entry_num, &last, last+1, 1, __ATOMIC_ACQUIRE, __ATOMIC_RELEASE));
is needed.
I want to clarity my understand is wrong or not.
本文标签: cstack without popis CAS with ATOMICRELEASE enough for itStack Overflow
版权声明:本文标题:c - stack without pop ... is CAS with __ATOMIC_RELEASE enough for it? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745584405a2664809.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论