Skip to content

kusl/readerwriter

Repository files navigation

ReaderWriter

Build, Test, and Run

A thread-safe implementation of the classic Reader-Writer synchronization problem in .NET 9.

Quick Start

Option 1: Download Pre-built Binary (No .NET Required)

Download the latest release for your operating system from the Releases page:

  • Windows: ReaderWriter-win-x64.zip
  • Linux: ReaderWriter-linux-x64.tar.gz
  • macOS: ReaderWriter-osx-x64.tar.gz

Extract and run:

# Windows
ReaderWriter.ConsoleApp.exe

# Linux/macOS
./ReaderWriter.ConsoleApp

Option 2: Build from Source

# Clone and build
git clone https://github.com/yourusername/readerwriter.git
cd readerwriter
dotnet build

# Run tests
dotnet test

# Run simulation
cd ReaderWriter.ConsoleApp
dotnet run

What It Does

Demonstrates concurrent access patterns where:

  • Multiple readers can access a shared resource simultaneously
  • Writers get exclusive access (no other readers or writers)
  • All access is thread-safe and logged

Project Structure

ReaderWriter.sln
├── ReaderWriter.Core/          # Thread-safe reader-writer logic
├── ReaderWriter.ConsoleApp/    # Simulation host
└── ReaderWriter.Tests/         # Unit tests

Configuration

Method 1: Edit appsettings.json

{
  "SimulationSettings": {
    "NumberOfReaders": 10,      # Concurrent readers
    "NumberOfWriters": 3,       # Concurrent writers
    "SimulationDurationSeconds": 30
  }
}

Method 2: Command Line Arguments

./ReaderWriter.ConsoleApp --readers 20 --writers 5 --duration 60
# or short form
./ReaderWriter.ConsoleApp -r 20 -w 5 -d 60

Method 3: Environment Variables

# Windows
set SimulationSettings__NumberOfReaders=25
set SimulationSettings__NumberOfWriters=3
set SimulationSettings__SimulationDurationSeconds=60
ReaderWriter.ConsoleApp.exe

# Linux/macOS
export SimulationSettings__NumberOfReaders=25
export SimulationSettings__NumberOfWriters=3
export SimulationSettings__SimulationDurationSeconds=60
./ReaderWriter.ConsoleApp

Sample Output

[20:42:57 INF] Writer 2 attempting to acquire write lock
[20:42:57 INF] Writer 2 acquired write lock
[20:42:57 INF] Writer 2 wrote data: Data from Writer 2 - Item 1. Total items: 1
[20:42:57 INF] Writer 2 released write lock
[20:42:57 INF] Reader 4 acquired read lock
[20:42:57 INF] Reader 7 acquired read lock
[20:42:57 INF] Reader 4 read data: Data from Writer 2 - Item 1
[20:42:57 INF] Reader 7 read data: Data from Writer 2 - Item 1

Running on Different Platforms

Windows PowerShell Script

.\ReaderWriter.ConsoleApp\localrun.ps1

Linux/macOS Bash Script

bash simple-run.sh

Building Platform-Specific Binaries

# Windows (self-contained, no .NET required)
dotnet publish -c Release -r win-x64 --self-contained -p:PublishSingleFile=true

# Linux (self-contained, no .NET required)
dotnet publish -c Release -r linux-x64 --self-contained -p:PublishSingleFile=true

# macOS (self-contained, no .NET required)
dotnet publish -c Release -r osx-x64 --self-contained -p:PublishSingleFile=true

# Framework-dependent (smaller size, requires .NET 9 runtime)
dotnet publish -c Release -r win-x64 --self-contained false -p:PublishSingleFile=true

CI/CD Pipeline

This project uses GitHub Actions to automatically:

  • Build and test on Windows, Linux, and macOS
  • Run the simulation with various configurations
  • Create platform-specific releases
  • Perform code analysis and security scanning

Every push and pull request triggers the full build pipeline, ensuring cross-platform compatibility.

Technical Details

  • Synchronization: ReaderWriterLockSlim (.NET built-in)
  • Logging: Serilog → Console + Daily rolling files
  • DI Container: Microsoft.Extensions.DependencyInjection
  • Testing: xUnit with high concurrency scenarios
  • Patterns: SOLID, DDD, Dependency Injection
  • Target Framework: .NET 9
  • Cross-Platform: Runs on Windows, Linux, and macOS

Test Coverage

  • ✅ Basic read/write operations
  • ✅ Writer blocks concurrent readers
  • ✅ Writer blocks concurrent writers
  • ✅ Multiple readers run concurrently
  • ✅ Data integrity under high concurrency
  • ✅ Cancellation and cleanup
  • ✅ Cross-platform compatibility

Performance Characteristics

  • Readers can operate concurrently for maximum throughput
  • Writers get exclusive access to maintain data consistency
  • Lock acquisition timeouts prevent deadlocks
  • Configurable operation delays simulate real-world scenarios

License

AGPL - See LICENSE.txt


Note: Implementation generated by Claude Opus 4 from specification by Gemini 2.5 Pro

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published