# saitejkuralla/semaphore architecture

> Architecture diagram of the GitHub repository saitejkuralla/semaphore, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-05-26). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/saitejkuralla/semaphore
- Repository: https://github.com/saitejkuralla/semaphore

## Overview

##### Project Type and Purpose
- Type: A multi-module console application suite (C#/.NET) illustrating thread synchronization patterns.
- Purpose: Provide real-world examples of Lock, Monitor, Semaphore, and SemaphoreSlim in isolated scenarios (BusReservation, SharedBankAccount, AtmAccess, LimitingCreditCard, LimitingServices).

##### File Structure
- Top-level solution: `Multi threading.sln`
- Five subprojects (each a console app with its own .csproj and Program.cs):
- BusReservationSystem
- SharedBankAccount
- AtmAccess
- LimitingCreditCard
- LimitingServices
- Each project lives in its own folder and references System.Threading primitives.

##### README for Insights
- Describes which synchronization primitive is exercised in each project.
- Documents the high-level workflow of threads, shared resources, and signaling/waiting.

##### Main Components & Interactions
a. Solution Container
- Represents the Visual Studio solution that groups the five demo modules.
b. Module (one per folder)
- Console App Host
- Thread Manager (creates/starts threads)
- Shared Resource (e.g., AvailableTickets, BankAccount.Balance, ATM machines, CreditCard queue, Service endpoints)
- Synchronization Primitive (Lock object, Monitor, Semaphore, SemaphoreSlim)
c. Threads
- Worker threads performing actions on the shared resource.
d. .NET Runtime
- Thread Scheduler, CLR, Threading Library (System.Threading namespace).

Interactions:
- Threads → request access via Sync Primitive → operate on Shared Resource → release Sync Primitive → continue.

##### Architectural Patterns & Principles
- Mutual Exclusion (Lock, Monitor.Enter/Exit)
- Counting Semaphore (Semaphore, SemaphoreSlim)
- Producer-Consumer / Signaling (Monitor.Wait/Pulse)
- Separation of concerns: each module is self-contained; no cross-module dependencies.

## Diagram (Mermaid)

```mermaid
flowchart TB
    %% Solution Container
    Solution["Multi threading.sln"]:::solution
    click Solution "https://github.com/saitejkuralla/semaphore/blob/main/Multi threading/Multi threading.sln"

    %% BusReservationSystem Module
    subgraph "BusReservationSystem" 
        B_ProjDir["BusReservationSystem"]:::module
        B_Program["Program.cs"]:::module
        B_Csproj["BusReservationSystem.csproj"]:::module
        B_Threads["Threads"]:::thread
        B_Primitive["Lock (lock obj)"]:::primitive
        B_Resource["AvailableTickets"]:::resource
    end
    click B_ProjDir "https://github.com/saitejkuralla/semaphore/tree/main/Multi threading/BusReservationSystem"
    click B_Program "https://github.com/saitejkuralla/semaphore/blob/main/Multi threading/BusReservationSystem/Program.cs"
    click B_Csproj "https://github.com/saitejkuralla/semaphore/blob/main/Multi threading/BusReservationSystem/BusReservationSystem.csproj"

    %% SharedBankAccount Module
    subgraph "SharedBankAccount"
        SB_ProjDir["SharedBankAccount"]:::module
        SB_Program["Program.cs"]:::module
        SB_Csproj["SharedBankAccount.csproj"]:::module
        SB_Threads["Threads"]:::thread
        SB_Primitive["Monitor (Enter/Wait/Pulse)"]:::primitive
        SB_Resource["BankAccount.Balance"]:::resource
    end
    click SB_ProjDir "https://github.com/saitejkuralla/semaphore/tree/main/Multi threading/SharedBankAccount"
    click SB_Program "https://github.com/saitejkuralla/semaphore/blob/main/Multi threading/SharedBankAccount/Program.cs"
    click SB_Csproj "https://github.com/saitejkuralla/semaphore/blob/main/Multi threading/SharedBankAccount/SharedBankAccount.csproj"

    %% AtmAccess Module
    subgraph "AtmAccess"
        ATM_ProjDir["AtmAccess"]:::module
        ATM_Program["Program.cs"]:::module
        ATM_Csproj["AtmAccess.csproj"]:::module
        ATM_Threads["Threads"]:::thread
        ATM_Primitive["Semaphore (WaitOne/Release)"]:::primitive
        ATM_Resource["ATM Machines"]:::resource
    end
    click ATM_ProjDir "https://github.com/saitejkuralla/semaphore/tree/main/Multi threading/AtmAccess"
    click ATM_Program "https://github.com/saitejkuralla/semaphore/blob/main/Multi threading/AtmAccess/Program.cs"
    click ATM_Csproj "https://github.com/saitejkuralla/semaphore/blob/main/Multi threading/AtmAccess/AtmAccess.csproj"

    %% LimitingCreditCard Module
    subgraph "LimitingCreditCard"
        CC_ProjDir["LimitingCreditCard"]:::module
        CC_Program["Program.cs"]:::module
        CC_Csproj["LimitingCreditCard.csproj"]:::module
        CC_Threads["Threads"]:::thread
        CC_Primitive["SemaphoreSlim (Wait/Release)"]:::primitive
        CC_Resource["CreditCard Queue"]:::resource
    end
    click CC_ProjDir "https://github.com/saitejkuralla/semaphore/tree/main/Multi threading/LimitingCreditCard"
    click CC_Program "https://github.com/saitejkuralla/semaphore/blob/main/Multi threading/LimitingCreditCard/Program.cs"
    click CC_Csproj "https://github.com/saitejkuralla/semaphore/blob/main/Multi threading/LimitingCreditCard/LimitingCreditCard.csproj"

    %% LimitingServices Module
    subgraph "LimitingServices"
        LS_ProjDir["LimitingServices"]:::module
        LS_Program["Program.cs"]:::module
        LS_Csproj["LimitingServices.csproj"]:::module
        LS_Threads["Threads"]:::thread
        LS_Primitive["SemaphoreSlim (Wait/Release)"]:::primitive
        LS_Resource["Service Endpoints"]:::resource
    end
    click LS_ProjDir "https://github.com/saitejkuralla/semaphore/tree/main/Multi threading/LimitingServices"
    click LS_Program "https://github.com/saitejkuralla/semaphore/blob/main/Multi threading/LimitingServices/Program.cs"
    click LS_Csproj "https://github.com/saitejkuralla/semaphore/blob/main/Multi threading/LimitingServices/LimitingServices.csproj"

    %% Documentation
    README["README.md"]:::doc
    click README "https://github.com/saitejkuralla/semaphore/blob/main/README.md"

    %% .NET Runtime Layer
    CLR[".NET CLR / Threading Library"]:::runtime

    %% Connections for each module
    B_Threads -->|"Acquire()/lock"| B_Primitive
    B_Primitive -->|"Guarded Access"| B_Resource
    B_Resource -->|"Release/Exit"| B_Primitive
    B_Primitive -->|"Unblock"| B_Threads

    SB_Threads -->|"Enter/Wait"| SB_Primitive
    SB_Primitive -->|"Guarded Access"| SB_Resource
    SB_Resource -->|"Pulse/Exit"| SB_Primitive
    SB_Primitive -->|"Unblock"| SB_Threads

    ATM_Threads -->|"WaitOne()"| ATM_Primitive
    ATM_Primitive -->|"Guarded Access"| ATM_Resource
    ATM_Resource -->|"Release()"| ATM_Primitive
    ATM_Primitive -->|"Unblock"| ATM_Threads

    CC_Threads -->|"Wait()"| CC_Primitive
    CC_Primitive -->|"Guarded Access"| CC_Resource
    CC_Resource -->|"Release()"| CC_Primitive
    CC_Primitive -->|"Unblock"| CC_Threads

    LS_Threads -->|"Wait()"| LS_Primitive
    LS_Primitive -->|"Guarded Access"| LS_Resource
    LS_Resource -->|"Release()"| LS_Primitive
    LS_Primitive -->|"Unblock"| LS_Threads

    %% All modules use CLR
    B_Primitive --> CLR
    SB_Primitive --> CLR
    ATM_Primitive --> CLR
    CC_Primitive --> CLR
    LS_Primitive --> CLR

    %% Styles
    classDef solution fill:#e3f2fd,stroke:#42a5f5,stroke-width:2px;
    classDef module fill:#bbdefb,stroke:#1e88e5,stroke-width:1px;
    classDef thread fill:#ffe0b2,stroke:#f57c00,stroke-width:1px,shape:circle;
    classDef primitive fill:#c8e6c9,stroke:#388e3c,stroke-width:1px,shape:hexagon;
    classDef resource fill:#fff9c4,stroke:#fdd835,stroke-width:1px,shape:ellipse;
    classDef runtime fill:#eceff1,stroke:#90a4ae,stroke-width:2px;
    classDef doc fill:#f3e5f5,stroke:#8e24aa,stroke-width:1px;
```
