Git Product home page Git Product logo

locked-queue's Introduction

Locked Queue uses locks and conditions to block when queue is empty, or it is full. Just as locks are inherently vulnerable to deadlock, Condition objects are inherently vulnerable to lost wakeups in which one or more threads wait forever without realizing that the condition for which they are waiting has become true.

This queue signals "not empty" whenever an item is added to the queue, and "not full" whenever an item is removed from the queue. However, consider an optimization, where you only signal "not empty" if the queue was empty. Bang! Lost wakeup is suddenly possible.

To see how that is possible, consider 2 consumers A & B and 2 producers C & D. When queue is empty and both A & B have to remove(), they are blocked until C or D can add(). If C add()s, followed by D, only 1 "not empty" condition would be active causing C to wakeup, but not D.

Hence, one needs to be careful when working with both locks and condition objects.

The functionality of this queue is similar to BlockingQueue and does not suffer from the lost wakeup problem.

Course: Concurrent Data Structures, Monsoon 2020
Taught by: Prof. Govindarajulu Regeti

add():
1. Acquire lock before any action.
2. Wait for queue being not full.
3. Add item to queue.
4. Release the lock.
remove():
1. Acquire lock before any action.
2. Wait for queue being not empty.
3. Remove item from queue.
4. Release the lock.

See LockedQueue.java for code, Main.java for test, and repl.it for output.

references

locked-queue's People

Contributors

wolfram77 avatar

Stargazers

 avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.