Search code examples
javamultithreadingqueuetheorywait-free

Wait-free queue implementation in JAVA


I've been trying to use already written Wait-free queue by Alex Kogan and Erez Petrank taken from here, but faced the problem. I can't understand that exactly thread ID need to be used in que() and deque() methods on page 5 and 7:

void enq(int value) {
    long phase = maxPhase() + 1; // cf. Figure 3b
    state.set(TID, new
        OpDesc(phase, true, true, new Node(value, TID)));
    help(phase);
    help finish enq();
}

And

int deq() throws EmptyException {
    long phase = maxPhase() + 1; // cf. Figure 5a
    state.set(TID, new OpDesc(phase, true, false, null));
    help(phase);
    help finish deq();
    Node node = state.get(TID).node;
    if (node == null) {
        throw new EmptyException();
    }
    return node.next.get().value;
}

What thread id should used?


Solution

  • In the paper they says that TID is an int in the range [0, . . . , NUM THRDS−1]., so it looks like it is a manually assigned number.

    I've not read the paper, so I don't know if there is a strict requirement only to use an identifier meeting this specification. There is a long getId() method on java.lang.Thread. See the Javadoc for Thread.getId():

    Returns the identifier of this Thread. The thread ID is a positive long number generated when this thread was created. The thread ID is unique and remains unchanged during its lifetime. When a thread is terminated, this thread ID may be reused.

    You may be able to use this instead of assigning the TIDs yourself.