Thread Skill — How to Work
Use this skill when the user asks about threads or concurrency: starting threads, thread pools, locks/mutex, race conditions, thread-safe code, or choosing between threads / processes / async.
Your Workflow
- 1. Clarify context: Identify language/runtime (Python, Java, C#, Node.js, Go, etc.) and what they want (I/O-bound vs CPU-bound, parallelism vs concurrency).
- Choose the right tool: Prefer the language’s recommended approach (e.g. async in Node/Python for I/O; threads or processes for CPU; see reference/concepts.md).
- Give the right level of detail:
-
Quick answer: Use the Quick Reference below and reply with a minimal example in their language.
-
Patterns and pitfalls: Point to or quote
reference/patterns.md (locks, queues, thread pool, shared state).
-
Language API: Point to
reference/languages.md or write a short snippet following that reference.
-
Templates: Use or adapt code in
assets/ when the user wants a starter or copy-paste pattern.
Quick Reference
| Goal | Prefer | Avoid |
|---|
| I/O-bound (network, disk) | Async (async/await, asyncio, Promise) or thread pool | Many blocking threads |
| CPU-bound (compute) |
Processes / process pool, or native threads (Java, Go, C#) | Many threads in Python (GIL) |
| Shared mutable state | Locks (mutex), thread-safe collections, or avoid sharing | Naked shared variables |
| Producer–consumer | Queue (thread-safe) | Busy-wait / ad-hoc signaling |
| Run N tasks in parallel | Thread pool or process pool | Manually creating N threads |
| Concept | One-liner |
|---|
| Race condition | Two threads read/write same data without synchronization; fix with lock or immutable design. |
| Deadlock |
Two (or more) threads wait for each other’s lock; fix by consistent lock order or timeouts. |
| GIL (Python) | Only one thread runs Python bytecode at a time; use processes or C extensions for CPU-bound. |
Where to Look
| Need | Location |
|---|
| When to use threads vs processes vs async | reference/concepts.md |
| Locks, queues, thread pool, shared state patterns |
reference/patterns.md |
| Python / Java / C# / Node / Go API quick ref |
reference/languages.md |
| Starter code / templates |
assets/ |
Safety and Best Practices
- - Do not suggest shared mutable state without synchronization (lock, queue, or thread-safe structure).
- Prefer higher-level constructs: thread pool, queue, async, instead of raw threads where they fit.
- For Python CPU-bound work, suggest multiprocessing or clarify GIL limits; for I/O-bound, suggest threading or asyncio as appropriate.
- When the user reports deadlock or race, point to reference/patterns.md and suggest minimal repro + lock discipline or queue-based design.
Assets
- - assets/python-thread-starter.py — Minimal Python threading + lock example.
- assets/python-thread-pool.py — Thread pool (e.g.
concurrent.futures) example.
Add or reference other language templates in assets/ when the user asks for a specific runtime.
线程技能 — 使用指南
当用户询问关于线程或并发的问题时使用此技能:包括启动线程、线程池、锁/互斥锁、竞态条件、线程安全代码,或在线程/进程/异步之间进行选择。
工作流程
- 1. 明确上下文:确定语言/运行时(Python、Java、C#、Node.js、Go 等)以及用户需求(I/O密集型 vs CPU密集型,并行 vs 并发)。
- 选择合适工具:优先采用语言推荐的方式(例如 Node/Python 中 I/O 使用异步;CPU 密集型使用线程或进程;参见 reference/concepts.md)。
- 提供适当详细程度:
-
快速回答:使用下方快速参考表,并用对应语言给出最小示例。
-
模式与陷阱:指向或引用
reference/patterns.md(锁、队列、线程池、共享状态)。
-
语言 API:指向
reference/languages.md 或按照该参考编写简短代码片段。
-
模板:当用户需要入门模板或可复制粘贴的模式时,使用或改编
assets/ 中的代码。
快速参考
| 目标 | 推荐 | 避免 |
|---|
| I/O密集型(网络、磁盘) | 异步(async/await、asyncio、Promise)或线程池 | 大量阻塞线程 |
| CPU密集型(计算) |
进程/进程池,或原生线程(Java、Go、C#) | Python 中大量线程(GIL) |
| 共享可变状态 | 锁(互斥锁)、线程安全集合,或避免共享 | 裸共享变量 |
| 生产者-消费者 | 队列(线程安全) | 忙等待/临时信号机制 |
| 并行执行 N 个任务 | 线程池或进程池 | 手动创建 N 个线程 |
| 概念 | 一句话说明 |
|---|
| 竞态条件 | 两个线程同时读写同一数据且未同步;通过加锁或不可变设计修复。 |
| 死锁 |
两个(或多个)线程互相等待对方释放锁;通过一致的加锁顺序或超时机制修复。 |
| GIL(Python) | 同一时刻只有一个线程执行 Python 字节码;CPU 密集型任务使用进程或 C 扩展。 |
查阅位置
reference/patterns.md |
| Python / Java / C# / Node / Go API 快速参考 |
reference/languages.md |
| 入门代码/模板 |
assets/ |
安全与最佳实践
- - 不要建议在没有同步机制(锁、队列或线程安全结构)的情况下使用共享可变状态。
- 优先使用高层构造:线程池、队列、异步,而非原始线程(在适用场景下)。
- 对于 Python 的 CPU 密集型任务,建议使用 multiprocessing 或说明 GIL 限制;对于 I/O 密集型任务,酌情建议使用 threading 或 asyncio。
- 当用户报告死锁或竞态条件时,指向 reference/patterns.md 并建议最小复现方案 + 锁规范或基于队列的设计。
资源文件
- - assets/python-thread-starter.py — 最小化 Python 线程 + 锁示例。
- assets/python-thread-pool.py — 线程池(例如 concurrent.futures)示例。
当用户询问特定运行时,可在 assets/ 中添加或引用其他语言模板。