Coder Social home page Coder Social logo

bqueue's Introduction

The Blocking Queue Module

The Blocking Queue Module provides an implementation of a Blocking Queue, which is an unbounded queue based on a linked list data structure. This Blocking Queue allows multiple threads to safely enqueue and dequeue elements concurrently, while also offering blocking operations when the queue is empty.

API

  1. put(data)

    • Description: Put an element into the queue.
    • Parameters:
      • data: The element to be put into the queue.
    • Return:
      • nil: If the element was successfully added to the queue.
      • error: If an error occurs during the operation.
  2. take(timeout?, t)

    • Description: Retrieve and remove an element from the head of the queue.
    • Parameters:
      • timeout (optional): The maximum time to wait for an element to become available. If not specified, this operation will block indefinitely.
      • t (optional): The type to which the element should be casted to.
    • Return:
      • The element that was removed from the queue if the operation is successful.
      • nil: On timeout when no element is available in the queue.
      • error: If the operation fails or when data binding to the specified type fails.
  3. peek(t)

    • Description: Retrieve, but do not remove, the head of the queue.
    • Parameters:
      • t: The type to which the element should be casted to.
    • Return:
      • The element retrieved from the queue, or nil if the queue is empty.
      • error: If data cannot be casted to the specified type.

Usage Example

import ballerina/io;
import ballerina/lang.runtime;

import crates/bqueue as bq;

public function main() returns error? {
    final bq:BlockingQueue bq = new;

    worker A returns error? {
        record {} data = check bq.take();
        io:println("Consumed data: ", data);
    }

    worker B returns error? {
        runtime:sleep(1);
        map<anydata> data = {value: 1, isPresent: true};
        check bq.put(data);
        io:println("Produced data: ", data);
    }

    check wait A;
}

bqueue's People

Contributors

mohamedsabthar avatar

Stargazers

Sameera Jayasoma avatar

Watchers

 avatar

bqueue's Issues

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.