csharp-concurrency-patterns

Recommends .NET concurrency constructs for given workload scenarios.

Updated Jan 29, 2024
One-click install
npx skills add https://github.com/riandeoliveira/aspnet-template --skill csharp-concurrency-patterns-riandeoliveira
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: csharp-concurrency-patterns
Source: https://github.com/riandeoliveira/aspnet-template/tree/main/.claude/skills/csharp-concurrency-patterns
Command: npx skills add https://github.com/riandeoliveira/aspnet-template --skill csharp-concurrency-patterns-riandeoliveira

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes references (resource) components.

What problem does it solve?

It helps you choose the right concurrency abstraction in .NET so you can avoid race conditions, deadlocks, and overly complex synchronization.

Core Features & Use Cases

  • Async-first decision making (async/await + Task.WhenAll/WhenAny): Use for I/O-bound work and coordinating independent asynchronous operations, with a focus on cancellation and avoiding blocking.
  • Parallelism with controlled concurrency (Parallel.ForEachAsync): Use for CPU-bound processing across collections while limiting concurrency to a safe degree.
  • Producer/consumer orchestration (System.Threading.Channels): Use for work queues and backpressure so producers and consumers can run at different speeds.
  • Escalation to advanced models (Rx, Akka.NET Streams, Akka.NET Actors): Use when you need UI/client event composition (Rx), server-side backpressure with batching/throttling (Akka.NET Streams), or stateful entity/state-machine management (Akka.NET Actors).

Quick Start

Use csharp-concurrency-patterns to decide whether your scenario is better handled by async/await, Parallel.ForEachAsync, Channels, Rx, or Akka.NET based on the type of workload and required guarantees.

Frequently Asked Questions about csharp-concurrency-patterns

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I choose the right C# concurrency pattern for my workload?

For async I/O operations, async/await with Task.WhenAll coordinates independent tasks while emphasizing cancellation and avoiding blocking to prevent deadlocks and race conditions.

When should I use System.Threading.Channels over async await in .NET?

Use System.Threading.Channels over async/await when building producer/consumer pipelines that require backpressure, allowing producers and consumers to run at different speeds without coupling their execution rates.

What is the best way to process CPU-bound collections concurrently in C#?

The best way to process CPU-bound collections concurrently in C# is Parallel.ForEachAsync, which enables controlled parallelism by limiting the degree of concurrency to a safe level during collection processing.

When do I need Reactive Extensions or Akka.NET for concurrent workflows?

Escalate to Reactive Extensions for UI event composition, Akka.NET Streams for server-side backpressure with batching, and Akka.NET Actors for stateful entity and state-machine management when basic async patterns are insufficient.

How do I avoid shared mutable state and synchronization deadlocks in C#?

Avoid shared mutable state and deadlocks by selecting appropriate concurrency abstractions over manual synchronization, emphasizing safe defaults like lock avoidance and proper cancellation token propagation.

Do I need external dependencies to use Parallel.ForEachAsync or Channel<T>?

You do not need external dependencies for Parallel.ForEachAsync or Channel<T> as they are built into .NET, but using Reactive Extensions or Akka.NET requires adding their respective libraries to your project.