Concurrency patterns

From aj-geddes/useful-ai-prompts · View interactive version

Concurrency patterns is an AI agent skill from the GitHub repository aj-geddes/useful-ai-prompts. Implement thread-safe code, mutexes, semaphores, async/await patterns, and concurrent data structures. Use when handling parallel operations, race conditions, or building high-performance concurrent systems.

How to use this skill

Skill file

Concurrency Patterns

Table of Contents

Overview

Implement safe concurrent code using proper synchronization primitives and patterns for parallel execution.

When to Use

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

❌ DON'T

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.