concurrencylinked-listatomiclock-free

Why do many implementations of lock-free linked list assume that items are unique in the list?


I am implementing my lock free linked list in C based on this repository and the chapter 9.8 of the book the art of multiprocessor programming. I think they are based on the Harris's paper. And I find many other implementations of lock free linked list only support unique items as these here, but why?

I hope that my implementation can contain duplicate items in the list. Based on this code, intuitively, I only comment the lines 111-113 that check whether the item already exists when inserting but that doesn't work. I want to know why this is not enough and what else modifications I should do. Or it would be nice if someone can share some resources regarding implementations supporting duplicate items.

Thanks in advance!


Solution

  • Back in days gone by—back when those authors were learning their craft—if we talked about a "linked list of objects," we usually meant that the objects were the nodes in the list. I.E., the "next" field (and, maybe a "prev" field) was a member of the object class itself. For example, if we wanted to keep a list of street addresses, we would declare,

    typedef struct street_address {
                         char* address_line_1;
                         char* address_line_2;
                         char* city;
                         char* state_or_province;
                         char* postal_code;
        struct street_address* next;
    } street_address_t;
    

    Then, to add a new street address to an existing list we might define something like,

    void add_street_address(
        street_address_t** head,
        street_address_t*  new_member
    ) {
        new_member->next = (*head);
        *head = new_member;
    }
    

    We didn't have generics back then. If we wanted to avoid duplicating code in programs that made lists of more than one type of object, then we either did it by type casting, or by use of macros.

    If we had meant to talk about a linked list of nodes that contained pointers to objects, then we would have explicitly said so. But, that would have been an unusual case. We would have thought it to be wasteful of memory and CPU power to make a list that way.

    The same node can never appear more than one time in a linked list of nodes. So, if the objects that you're storing are the nodes, then obviously, the same object can not appear in the list more than one time.

    In modern libraries, where the nodes are separate things that contain pointers to the "objects," then you can have any number of distinct nodes in the list that all point to the same object.