From f8c040bac661a361dbd8a2c2073972ed57033fd7 Mon Sep 17 00:00:00 2001 From: Mehul Prajapati Date: Sun, 13 Oct 2024 10:48:40 -0400 Subject: [PATCH 1/3] added concurrent queue --- docs/Queue/concurrent-queue.md | 92 ++++++++++++++++++++++++++++++++++ 1 file changed, 92 insertions(+) create mode 100644 docs/Queue/concurrent-queue.md diff --git a/docs/Queue/concurrent-queue.md b/docs/Queue/concurrent-queue.md new file mode 100644 index 000000000..a946e6d53 --- /dev/null +++ b/docs/Queue/concurrent-queue.md @@ -0,0 +1,92 @@ +# Concurrent Queue Data Structure + +A concurrent queue is a thread-safe data structure designed for safe access by multiple threads. It allows efficient queue operations while ensuring data integrity and preventing race conditions. + +## Introduction + +A concurrent queue follows the First In First Out (FIFO) principle, allowing multiple threads to perform enqueue and dequeue operations safely without explicit locks. This makes it ideal for multithreading environments. + +## Operations + +1. **Enqueue**: Add an element to the back of the queue. +2. **Dequeue**: Remove the element from the front of the queue. +3. **Peek**: Retrieve the element at the front of the queue without removing it. +4. **isEmpty**: Check if the queue is empty. +5. **isFull**: Check if the queue is full. +6. **Size**: Get the number of elements in the queue. + +## Implementation + +### Python Code + +```python +import threading + +class ConcurrentQueue: + def __init__(self, size): + self.size = size + self.elements = [None] * size + self.front = 0 + self.rear = 0 + self.lock = threading.Lock() + + def enqueue(self, element): + with self.lock: + if self.is_full(): + return "Queue Overflow" + self.rear = (self.rear + 1) % self.size + self.elements[self.rear] = element + + def dequeue(self): + with self.lock: + if self.is_empty(): + return "Queue Underflow" + frontElement = self.elements[self.front] + self.front = (self.front + 1) % self.size + return frontElement + + def peek(self): + with self.lock: + if self.is_empty(): + return "Queue is empty" + return self.elements[self.front] + + def is_empty(self): + with self.lock: + return self.front == self.rear + + def is_full(self): + with self.lock: + return (self.rear + 1) % self.size == self.front + + def size_of_queue(self): + with self.lock: + return (self.rear - self.front + self.size) % self.size + +# Example usage +if __name__ == "__main__": + cq = ConcurrentQueue(5) + cq.enqueue(10) + cq.enqueue(20) + print(cq.dequeue()) # Output: 10 + print(cq.peek()) # Output: 20 + print(cq.is_empty()) # Output: False + print(cq.size_of_queue()) # Output: 1 +``` + +## Complexity + +- **Time Complexity**: + - **Enqueue**: \(O(1)\) + - **Dequeue**: \(O(1)\) + - **Peek**: \(O(1)\) + - **isEmpty**: \(O(1)\) + - **isFull**: \(O(1)\) + - **Size**: \(O(1)\) + +- **Space Complexity**: \(O(n)\), where \(n\) is the maximum number of elements that can be stored in the queue. + +## Conclusion + +The concurrent queue is an efficient data structure that simplifies multithreading operations by automatically managing synchronization. It is particularly useful in scenarios such as task scheduling, producer-consumer problems, and buffering tasks in concurrent systems. By ensuring thread safety and minimizing the need for explicit locks, it enhances performance and simplifies the design of concurrent applications. + From 88b7dc5e4423e69e89ee43e8523fe08bc005a9f8 Mon Sep 17 00:00:00 2001 From: Mehul Prajapati Date: Sun, 13 Oct 2024 10:50:41 -0400 Subject: [PATCH 2/3] added image link --- docs/Queue/concurrent-queue.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/docs/Queue/concurrent-queue.md b/docs/Queue/concurrent-queue.md index a946e6d53..f980d3701 100644 --- a/docs/Queue/concurrent-queue.md +++ b/docs/Queue/concurrent-queue.md @@ -6,6 +6,8 @@ A concurrent queue is a thread-safe data structure designed for safe access by m A concurrent queue follows the First In First Out (FIFO) principle, allowing multiple threads to perform enqueue and dequeue operations safely without explicit locks. This makes it ideal for multithreading environments. +![](https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcQWItU6BP0_HnsEQmw-ofD-LWef-uGJSoNvJQ&s) + ## Operations 1. **Enqueue**: Add an element to the back of the queue. From 26e7be7cd3b71038d9eefdbfca8d6f7909bed407 Mon Sep 17 00:00:00 2001 From: Mehul Prajapati Date: Tue, 15 Oct 2024 11:41:08 -0400 Subject: [PATCH 3/3] Update concurrent-queue.md --- docs/Queue/concurrent-queue.md | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/docs/Queue/concurrent-queue.md b/docs/Queue/concurrent-queue.md index f980d3701..7c9a5260c 100644 --- a/docs/Queue/concurrent-queue.md +++ b/docs/Queue/concurrent-queue.md @@ -1,3 +1,12 @@ +--- +id: concurrent-queue-data-structure +title: "Concurrent Queue Data Structure" +sidebar_label: "Concurrent Queue" +sidebar_position: 8 +description: "A comprehensive guide to using the concurrent queue data structure for safe multithreading operations." +tags: [data structure, concurrency, multithreading] +--- + # Concurrent Queue Data Structure A concurrent queue is a thread-safe data structure designed for safe access by multiple threads. It allows efficient queue operations while ensuring data integrity and preventing race conditions.