File contents Concurrency Patterns
Table of Contents
Overview
Implement safe concurrent code using proper synchronization primitives and patterns for parallel execution.
When to Use
Multi-threaded applications
Parallel data processing
Race condition prevention
Resource pooling
Task coordination
High-performance systems
Async operations
Worker pools
Quick Start
Minimal working example:
class PromisePool {
private queue: Array<() => Promise<any>> = [];
private active = 0;
constructor(private concurrency: number) {}
async add<T>(fn: () => Promise<T>): Promise<T> {
while (this.active >= this.concurrency) {
await this.waitForSlot();
}
this.active++;
try {
return await fn();
} finally {
this.active--;
}
}
private async waitForSlot(): Promise<void> {
return new Promise((resolve) => {
const checkSlot = () => {
if (this.active < this.concurrency) {
resolve();
// ... (see reference guides for full implementation)
Reference Guides
Detailed implementations in the references/ directory:
Guide
Contents
Promise Pool (TypeScript)
Promise Pool (TypeScript)
Mutex and Semaphore (TypeScript)
Mutex and Semaphore (TypeScript)
Worker Pool (Node.js)
Worker Pool (Node.js)
Python Threading Patterns
Python Threading Patterns
Async Patterns (Python asyncio)
Async Patterns (Python asyncio)
Go-Style Channels (Simulation)
Go-Style Channels (Simulation)
Best Practices
✅ DO
Use proper synchronization primitives
Limit concurrency to avoid resource exhaustion
Handle errors in concurrent operations
Use immutable data when possible
Test concurrent code thoroughly
Profile concurrent performance
Document thread-safety guarantees
❌ DON'T
Share mutable state without synchronization
Use sleep/polling for coordination
Create unlimited threads/workers
Ignore race conditions
Block event loops in async code
Forget to clean up resources
Converted and distributed by TomeVault — claim your Tome and manage your conversions.
1 --- 2 name: aj-geddes-useful-ai-prompts-concurrency-patterns 3 description: Concurrency Patterns 4 --- 5 6 # Concurrency Patterns 7 8 ## Table of Contents 9 10 - [Overview](#overview) 11 - [When to Use](#when-to-use) 12 - [Quick Start](#quick-start) 13 - [Reference Guides](#reference-guides) 14 - [Best Practices](#best-practices) 15 16 ## Overview 17 18 Implement safe concurrent code using proper synchronization primitives and patterns for parallel execution. 19 20 ## When to Use 21 22 - Multi-threaded applications 23 - Parallel data processing 24 - Race condition prevention 25 - Resource pooling 26 - Task coordination 27 - High-performance systems 28 - Async operations 29 - Worker pools 30 31 ## Quick Start 32 33 Minimal working example: 34 35 ```typescript 36 class PromisePool { 37 private queue: Array<() => Promise<any>> = []; 38 private active = 0; 39 40 constructor(private concurrency: number) {} 41 42 async add<T>(fn: () => Promise<T>): Promise<T> { 43 while (this.active >= this.concurrency) { 44 await this.waitForSlot(); 45 } 46 47 this.active++; 48 49 try { 50 return await fn(); 51 } finally { 52 this.active--; 53 } 54 } 55 56 private async waitForSlot(): Promise<void> { 57 return new Promise((resolve) => { 58 const checkSlot = () => { 59 if (this.active < this.concurrency) { 60 resolve(); 61 // ... (see reference guides for full implementation) 62 ``` 63 64 ## Reference Guides 65 66 Detailed implementations in the `references/` directory: 67 68 | Guide | Contents | 69 |---|---| 70 | [Promise Pool (TypeScript)](references/promise-pool-typescript.md) | Promise Pool (TypeScript) | 71 | [Mutex and Semaphore (TypeScript)](references/mutex-and-semaphore-typescript.md) | Mutex and Semaphore (TypeScript) | 72 | [Worker Pool (Node.js)](references/worker-pool-nodejs.md) | Worker Pool (Node.js) | 73 | [Python Threading Patterns](references/python-threading-patterns.md) | Python Threading Patterns | 74 | [Async Patterns (Python asyncio)](references/async-patterns-python-asyncio.md) | Async Patterns (Python asyncio) | 75 | [Go-Style Channels (Simulation)](references/go-style-channels-simulation.md) | Go-Style Channels (Simulation) | 76 77 ## Best Practices 78 79 ### ✅ DO 80 81 - Use proper synchronization primitives 82 - Limit concurrency to avoid resource exhaustion 83 - Handle errors in concurrent operations 84 - Use immutable data when possible 85 - Test concurrent code thoroughly 86 - Profile concurrent performance 87 - Document thread-safety guarantees 88 89 ### ❌ DON'T 90 91 - Share mutable state without synchronization 92 - Use sleep/polling for coordination 93 - Create unlimited threads/workers 94 - Ignore race conditions 95 - Block event loops in async code 96 - Forget to clean up resources 97 98 --- 99 > Converted and distributed by [TomeVault](https://tomevault.io/claim/aj-geddes) — claim your Tome and manage your conversions. 100 <!-- tomevault:4.0:skill_md:2026-04-11 -->
tomevault-io/skills-registry/tree/main/aj-geddes--useful-ai-prompts--concurrency-patterns commit 024eef8006
Frequently asked questions How do I install the Aj Geddes Useful AI Prompts Concurrency Patterns skill? Run npx skillmds@latest add tomevault-io/aj-geddes-useful-ai-prompts-concurrency-patterns in your terminal (requires Node.js), paste this page's agent-chat prompt into Claude, Cursor, or any MCP-connected agent, or download the SKILL.md file and copy it into your agent's skills directory.
What does the Aj Geddes Useful AI Prompts Concurrency Patterns skill do? Concurrency Patterns It is listed under Coding & Dev Tools on SkillMD.
Is Aj Geddes Useful AI Prompts Concurrency Patterns safe to use? This skill has not completed SkillMD's automated safety review yet. Independent scanners report: SkillSpector: PASS, Skill Scanner: PASS. SkillMD never runs a skill's scripts for you; review the SKILL.md before installing.
Which AI agents work with Aj Geddes Useful AI Prompts Concurrency Patterns? This skill is tagged as working with Claude Code, Claude.ai, OpenAI Codex. SKILL.md is an open format, so most agents that read a skills directory can load it too.
Is Aj Geddes Useful AI Prompts Concurrency Patterns free to use? Yes. Installing skills from SkillMD is free, and the skill stays under its author's original license.
Who published Aj Geddes Useful AI Prompts Concurrency Patterns? tomevault-io (@tomevault-io) published this skill. Their other Agent Skills are listed on their SkillMD profile.