I've been studying the lock free stack.
I was reading this link and the pop function got me wondering.
根据链接,弹出功能实现如下.
int lfstack_pop(_Atomic lfstack_t *lfstack)
{
lfstack_t next, orig = atomic_load(lfstack);
do{
if(orig.head == NULL)
{
return -1;
}
next.head = orig.head->next;
next.tag = orig.tag+1; //increase the "tag"
}while(!atomic_compare_exchange_weak(lfstack,&orig,next));
printf("poping value %d\n",orig.head->data);
free(orig.head);
return 0;
}
In the above implementation, we are solving the ABA problem by using separate tag information.
So far, so good.
But my question is this.
Is the line next.head = orig.head->next;
thread-safe?
如果行next.head = orig.head->next;
在线程1中的free(orig.head);
之后在线程2中继续,这不是一个错误吗,因为我们在orig.head
被释放之后引用next
?
我正在查看的链接没有提到这个问题,所以我想知道这是否安全.