Concurrency Patterns

Concurrency Patterns

Teaches an AI agent to write thread-safe concurrent code with mutexes, semaphores, promise pools, worker pools and asyncio, avoiding race conditions.

Skill files9 files

references/

scripts/

templates/

SKILL.md100 lines
1
---
2
name: concurrency-patterns
3
description: >
4
Implement thread-safe code, mutexes, semaphores, async/await patterns, and
5
concurrent data structures. Use when handling parallel operations, race
6
conditions, or building high-performance concurrent systems.
7
---
8
9
# Concurrency Patterns
10
11
## Table of Contents
12
13
- [Overview](#overview)
14
- [When to Use](#when-to-use)
15
- [Quick Start](#quick-start)
16
- [Reference Guides](#reference-guides)
17
- [Best Practices](#best-practices)
18
19
## Overview
20
21
Implement safe concurrent code using proper synchronization primitives and patterns for parallel execution.
22
23
## When to Use
24
25
- Multi-threaded applications
26
- Parallel data processing
27
- Race condition prevention
28
- Resource pooling
29
- Task coordination
30
- High-performance systems
31
- Async operations
32
- Worker pools
33
34
## Quick Start
35
36
Minimal working example:
37
38
```typescript
39
class PromisePool {
40
private queue: Array<() => Promise<any>> = [];
41
private active = 0;
42
43
constructor(private concurrency: number) {}
44
45
async add<T>(fn: () => Promise<T>): Promise<T> {
46
while (this.active >= this.concurrency) {
47
await this.waitForSlot();
48
}
49
50
this.active++;
51
52
try {
53
return await fn();
54
} finally {
55
this.active--;
56
}
57
}
58
59
private async waitForSlot(): Promise<void> {
60
return new Promise((resolve) => {
61
const checkSlot = () => {
62
if (this.active < this.concurrency) {
63
resolve();
64
// ... (see reference guides for full implementation)
65
```
66
67
## Reference Guides
68
69
Detailed implementations in the `references/` directory:
70
71
| Guide | Contents |
72
|---|---|
73
| [Promise Pool (TypeScript)](references/promise-pool-typescript.md) | Promise Pool (TypeScript) |
74
| [Mutex and Semaphore (TypeScript)](references/mutex-and-semaphore-typescript.md) | Mutex and Semaphore (TypeScript) |
75
| [Worker Pool (Node.js)](references/worker-pool-nodejs.md) | Worker Pool (Node.js) |
76
| [Python Threading Patterns](references/python-threading-patterns.md) | Python Threading Patterns |
77
| [Async Patterns (Python asyncio)](references/async-patterns-python-asyncio.md) | Async Patterns (Python asyncio) |
78
| [Go-Style Channels (Simulation)](references/go-style-channels-simulation.md) | Go-Style Channels (Simulation) |
79
80
## Best Practices
81
82
### ✅ DO
83
84
- Use proper synchronization primitives
85
- Limit concurrency to avoid resource exhaustion
86
- Handle errors in concurrent operations
87
- Use immutable data when possible
88
- Test concurrent code thoroughly
89
- Profile concurrent performance
90
- Document thread-safety guarantees
91
92
### ❌ DON'T
93
94
- Share mutable state without synchronization
95
- Use sleep/polling for coordination
96
- Create unlimited threads/workers
97
- Ignore race conditions
98
- Block event loops in async code
99
- Forget to clean up resources
100

© AJ Geddes · MIT · source · posted by @OpenSourceBuffGuy on promptparty.com

@OpenSourceBuffGuy

I find openly licensed prompts and post them here.

0 followers · 13,696 prompts

More by @OpenSourceBuffGuy

See all
Nine panel morning routine photo dump carousel prompt image

Nine panel morning routine photo dump carousel

Makes a 3x3 Instagram-style collage of one woman's messy, candid morning routine, panel by panel, with a negative prompt to avoid glamour and plastic skin.

image-prompt, nano-banana, negative-prompt, character-consistency, collage, multi-panel, social-media-graphic, candid, photorealistic, smartphone-photo, natural-light, soft-lighting, woman, muted-colors, selfie, square-format, instagram, for-content-creators, text-rendering, long-form, beauty, cozy, portrait

Three frame underwater movie poster of sinking man prompt image

Three frame underwater movie poster of sinking man

Draws a cinematic three-frame underwater movie poster of a man sinking, moving from close-up to wide shot with light beams, bubbles and a short title.

image-prompt, nano-banana, poster, ocean, cinematic, multi-panel, close-up, medium-shot, wide-shot, dramatic-lighting, photorealistic, moody, bokeh, text-rendering, vertical-format, man, portrait, blue-hour, soft-lighting, muted-colors, for-filmmakers, for-designers, short-film

A summer evening in Seoul, shot like a home video prompt image

A summer evening in Seoul, shot like a home video

Generates a 30-second home-video style clip of a young woman on a summer evening in a Seoul neighborhood, shot like early-2000s camcorder footage with small accidents and natural sound only.

video-prompt, home-video, vlog, handheld, film-grain, candid, korea, multi-shot, ugc-video, lip-sync, guardrails, seedance, long-form, natural-light, vintage, muted-colors, cityscape, for-content-creators, sound-effects, structured-output, summer, timed-beats

Reviews

Loading reviews…

Discussion

Loading comments…