cs-concurrency

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

cs-concurrency

cs-concurrency

Purpose

用途

This skill equips OpenClaw to assist with concurrency concepts in computer science, including threads vs. async programming, synchronization primitives like locks and atomics, and advanced topics like actor models, STM, and deadlock avoidance. Use it to generate code, explain pitfalls, or debug issues.
该技能为OpenClaw提供计算机科学中的并发概念支持,包括线程与异步编程的对比、锁和原子操作等同步原语,以及Actor模型、STM(软件事务内存)和死锁规避等进阶主题。可用于生成代码、解释陷阱或调试问题。

When to Use

使用场景

Apply this skill when developing multi-threaded applications (e.g., in C++ or Python), handling shared resources to prevent race conditions, optimizing for I/O-bound tasks with async/await, or analyzing deadlocks in production code. Use it for real-time systems, web servers, or distributed computing where concurrency is critical.
在开发多线程应用(如C++或Python环境下)、处理共享资源以防止竞态条件、使用async/await优化I/O密集型任务,或分析生产代码中的死锁问题时,可应用此技能。适用于实时系统、Web服务器或对并发要求较高的分布式计算场景。

Key Capabilities

核心能力

  • Explain differences: Threads (blocking, OS-level) vs. async (non-blocking, event-loop based).
  • Demonstrate synchronization: Implement mutexes, RW locks, and atomics for shared data.
  • Handle advanced patterns: Generate actor model code (e.g., using Erlang-style actors) or STM for transactional memory.
  • Detect issues: Identify potential deadlocks or race conditions in provided code snippets.
  • Optimize: Suggest lock-free data structures like concurrent queues.
  • 解释差异:线程(阻塞式、操作系统级)与异步(非阻塞式、基于事件循环)的区别。
  • 演示同步:为共享数据实现互斥锁、读写锁和原子操作。
  • 处理进阶模式:生成Actor模型代码(如Erlang风格的Actor)或用于事务内存的STM实现。
  • 检测问题:识别提供的代码片段中潜在的死锁或竞态条件。
  • 优化建议:推荐无锁数据结构,如并发队列。

Usage Patterns

使用模式

Invoke OpenClaw via CLI for quick explanations or code generation; use API for integration into scripts. Always specify the subtopic (e.g., "threads" or "locks") for targeted responses. For interactive sessions, prefix commands with "openclaw cs-concurrency". If using programmatically, pass JSON payloads with required parameters like topic and language.
通过CLI调用OpenClaw以快速获取解释或生成代码;通过API将其集成到脚本中。请始终指定子主题(如“threads”或“locks”)以获取针对性响应。在交互式会话中,命令前缀为“openclaw cs-concurrency”。若以编程方式使用,需传递包含主题、语言等必填参数的JSON负载。

Common Commands/API

常用命令/API

Use CLI commands like:
openclaw cs-concurrency explain threads --lang python
(explains threads with a Python example).
openclaw cs-concurrency generate lock --type mutex --code c++
(generates a mutex example in C++).
For API, send POST requests to
/api/cs-concurrency/explain
with JSON body:
{ "topic": "atomics", "lang": "rust", "detail": "high" }

Headers:
Authorization: Bearer $OPENCLAW_API_KEY
(set via environment variable for authentication).
Config format for custom sessions:
JSON file like
{ "defaultLang": "go", "focus": ["deadlock", "async"] }
passed with
--config path/to/file.json
.
使用如下CLI命令:
openclaw cs-concurrency explain threads --lang python
(结合Python示例解释线程)。
openclaw cs-concurrency generate lock --type mutex --code c++
(生成C++环境下的互斥锁示例)。
对于API,向
/api/cs-concurrency/explain
发送POST请求,JSON请求体如下:
{ "topic": "atomics", "lang": "rust", "detail": "high" }

请求头:
Authorization: Bearer $OPENCLAW_API_KEY
(通过环境变量设置以完成身份验证)。
自定义会话的配置格式:
JSON文件示例:
{ "defaultLang": "go", "focus": ["deadlock", "async"] }
,通过
--config path/to/file.json
参数传入。

Integration Notes

集成说明

Integrate by setting $OPENCLAW_API_KEY in your environment for authenticated API calls. For example, in a bash script:
export OPENCLAW_API_KEY=your_api_key_here
. Combine with other tools like debuggers (e.g., gdb for threads) by piping output:
openclaw cs-concurrency explain deadlock | gdb -ex "run"
. Ensure your application handles async contexts if embedding responses in Node.js or Python event loops.
通过在环境中设置$OPENCLAW_API_KEY来集成,以进行已验证的API调用。例如,在bash脚本中:
export OPENCLAW_API_KEY=your_api_key_here
。可与调试工具(如用于线程调试的gdb)结合使用,通过管道传递输出:
openclaw cs-concurrency explain deadlock | gdb -ex "run"
。若在Node.js或Python事件循环中嵌入响应,请确保应用程序能处理异步上下文。

Error Handling

错误处理

When using this skill, check for concurrency errors like deadlocks by wrapping code in try-except blocks. For example, in Python:
python
import threading
lock = threading.Lock()
try:
    with lock:
        # Critical section
        pass
except threading.LockError:
    print("Lock acquisition failed")
For API calls, handle HTTP errors (e.g., 401 for invalid $OPENCLAW_API_KEY) by checking response status codes. In OpenClaw commands, use
--verbose
flag to debug:
openclaw cs-concurrency explain async --verbose
to log detailed errors.
使用此技能时,可通过将代码包裹在try-except块中来检查死锁等并发错误。例如,在Python中:
python
import threading
lock = threading.Lock()
try:
    with lock:
        # 临界区
        pass
except threading.LockError:
    print("锁获取失败")
对于API调用,通过检查响应状态码来处理HTTP错误(如无效$OPENCLAW_API_KEY导致的401错误)。在OpenClaw命令中,使用
--verbose
标志进行调试:
openclaw cs-concurrency explain async --verbose
以记录详细错误信息。

Concrete Usage Examples

实际使用示例

Example 1: To generate a simple threads example in C++:
Run:
openclaw cs-concurrency generate threads --lang c++

Output might include:
cpp
#include <thread>
void task() { /* code */ }
int main() {
    std::thread t1(task);
    t1.join();
}
This helps in understanding basic thread creation and joining.
Example 2: To explain and fix a race condition with atomics:
Command:
openclaw cs-concurrency explain race --fix --lang rust

Response: Explains the issue and provides:
rust
use std::sync::atomic::{AtomicUsize, Ordering};
static COUNTER: AtomicUsize = AtomicUsize::new(0);
COUNTER.fetch_add(1, Ordering::SeqCst);
Use this to safely increment shared counters without locks.
示例1:生成C++环境下的简单线程示例
运行命令:
openclaw cs-concurrency generate threads --lang c++

输出示例:
cpp
#include <thread>
void task() { /* 代码 */ }
int main() {
    std::thread t1(task);
    t1.join();
}
该示例有助于理解线程的创建与基本使用。
示例2:解释并修复原子操作相关的竞态条件
命令:
openclaw cs-concurrency explain race --fix --lang rust

响应:解释问题并提供代码示例:
rust
use std::sync::atomic::{AtomicUsize, Ordering};
static COUNTER: AtomicUsize = AtomicUsize::new(0);
COUNTER.fetch_add(1, Ordering::SeqCst);
使用此代码可在无需锁的情况下安全地递增共享计数器。

Graph Relationships

关联关系

  • Related to: cs-algorithms (via concurrency tag for parallel algorithms)
  • Connected to: programming-languages (shares tags like "threads" for language-specific implementations)
  • Links with: software-engineering (for deadlock in system design)
  • Associated via: cs (cluster overlap for computer science topics)
  • 关联技能:cs-algorithms(通过并发标签关联并行算法)
  • 关联技能:programming-languages(共享“threads”等语言特定实现标签)
  • 关联技能:software-engineering(涉及系统设计中的死锁问题)
  • 关联集群:cs(计算机科学主题的集群重叠)