Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Problem with snapshot in lock-free set

  1. #1
    Junior Member
    Join Date
    Jun 2022
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Angry Problem with snapshot in lock-free set

    I wrote a lock-free set. It was a lab assignment.
    The teacher said that there is a problem in the implementation of snapshot. It is necessary to give an example of how to achieve this effect.
    He said that I need to find an option when two elements always exist in turn in the list, and the snapshot says that they were never in the list

    I've already lost all hope of coming up with an example

    My code:
    public class Node<T extends Comparable<T>> {
        private final T value;
        private final AtomicMarkableReference<Node<T>> nextNode;
     
        public Node(final T value, final AtomicMarkableReference<Node<T>> nextNode) {
            this.value = value;
            this.nextNode = nextNode;
        }
     
        public Node(final T value) {
            this(value, new AtomicMarkableReference<>(null, false));
        }
     
        public T getValue() {
            return value;
        }
     
        public AtomicMarkableReference<Node<T>> getNextNode() {
            return nextNode;
        }
    }
     
    public class SetImpl<T extends Comparable<T>> {
        private final AtomicMarkableReference<Node<T>> head;
     
        public SetImpl() {
            this.head = new AtomicMarkableReference<>(new Node<>(null), false);
        }
     
        public boolean add(final T value) {
            final var newNode = new Node<>(value);
     
            while (true) {
                final var nodePosition = findNode(value);
     
                if (nodePosition.getRight() != null) {
                    return false;
                }
     
                if (nodePosition.getLeft().getNextNode().compareAndSet(null, newNode, false, false)) {
                    return true;
                }
            }
        }
     
        public boolean remove(final T value) {
            while (true) {
                final var nodePosition = findNode(value);
     
                if (nodePosition.getRight() == null) {
                    return false;
                }
     
                final var nextNode = nodePosition.getRight().getNextNode().getReference();
                if (nodePosition.getRight().getNextNode().compareAndSet(nextNode, nextNode, false, true)) {
                    nodePosition.getLeft().getNextNode().compareAndSet(nodePosition.getRight(), nextNode, false, false);
                    return true;
                }
            }
        }
     
        public boolean contains(final T value) {
            return findNode(value).getRight() != null;
        }
     
        public boolean isEmpty() {
            return makeSnapshot().isEmpty();
        }
     
        private Pair<Node<T>, Node<T>> findNode(final T value) {
            Node<T> previousNode;
            Node<T> currentNode;
     
            for (
                previousNode = head.getReference(), currentNode = previousNode.getNextNode().getReference();
                currentNode != null;
                previousNode = currentNode, currentNode = currentNode.getNextNode().getReference()
            ) {
                if (currentNode.getNextNode().isMarked()) {
                    boolean marked = previousNode.getNextNode().isMarked();
                    previousNode.getNextNode().compareAndSet(currentNode, currentNode.getNextNode().getReference(), marked, marked);
                }
     
                if (!currentNode.getNextNode().isMarked() && value.equals(currentNode.getValue())) {
                    return new ImmutablePair<>(previousNode, currentNode);
                }
            }
     
            return new ImmutablePair<>(previousNode, null);
        }
     
        private Collection<T> makeSnapshot() {
            List<Node<T>> nodesV1;
            List<Node<T>> nodesV2;
     
            do {
                nodesV1 = getCurrentNodes();
                nodesV2 = getCurrentNodes();
            } while (!nodesV1.equals(nodesV2));
     
            return nodesV1.stream().map(Node::getValue).collect(Collectors.toList());
        }
     
        private List<Node<T>> getCurrentNodes() {
            final List<Node<T>> result = new ArrayList<>();
     
            for (
                Node<T> node = head.getReference().getNextNode().getReference();
                node != null;
                node = node.getNextNode().getReference()
            ) {
                if (!node.getNextNode().isMarked()) {
                    result.add(node);
                }
            }
     
            return result;
        }
    }

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Problem with snapshot in lock-free set

    Is there some output that you can post to show what you are asking about?
    Be sure to add some comments where the problem is that describe the problem.

    Where is the main method that is needed for testing the code?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Jun 2022
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Problem with snapshot in lock-free set

    Quote Originally Posted by Norm View Post
    Is there some output that you can post to show what you are asking about?
    Be sure to add some comments where the problem is that describe the problem.
    The problem is not permanent.
    it consists in the mismatch of the internal state and the state of the snapshot
    And if I understand correctly, one of the elements is always in the list

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Problem with snapshot in lock-free set

    How can the code be executed to see the problem?

    What is the snapshot that you talk of?
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. How to open the lock?
    By 19xx62xx14 in forum What's Wrong With My Code?
    Replies: 8
    Last Post: October 4th, 2021, 05:30 AM
  2. Replies: 0
    Last Post: October 14th, 2013, 08:30 PM
  3. Class level Lock vs Object level lock
    By hs82 in forum Java Theory & Questions
    Replies: 0
    Last Post: October 14th, 2013, 10:17 AM
  4. Snapshot sequence
    By AnilS23 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: November 13th, 2012, 08:35 AM

Tags for this Thread