Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Traversing a list with hazard pointers

I'm working with Hazard pointer in order to implement a lock-free linked list in C. I couldn't find any example code other than vary basics queues and stacks. The problem is I need to traverse the list, so my question is if I can change the value of a hazard pointer once is assigned. For example:

t←Top
while(true) {
    if t=null then
        return null
    *hp←t
    if Top!=t then
        continue
    ...
    t←(t→next) //after this instruction pointer t will be still protected?
}
like image 620
ees_cu Avatar asked Dec 03 '13 21:12

ees_cu


Video Answer


1 Answers

Finally I ended implementing my own version of Hazard Pointers (HP) according to the original paper. The answer to my question is NO, t is no longer safe to use. The reason is that, the way HP works, *hp is protecting the node being pointed by t when you declared it as a hazardous pointer, so when t moves to the next node, the HP mechanism keeps protecting the previous node. I have to reassign the new value to *hp before I can use it safely.

Also, in the example of the paper it is not explicit, but when you finish using a hazard pointer you have to release it. That means, return *hp to its original state (NULL). This way, if another thread wants to delete (retire) this node, it won't be seen as being used.

In my example above, I have to release *hp before leaving the method. Inside the loop it is not necessary because I am overwriting the same *hp position (*hp ← t), so the previous node is no longer protected.

like image 138
ees_cu Avatar answered Oct 12 '22 13:10

ees_cu