plugins/languages/c/skills/concurrency/SKILL.md
C concurrency conventions: C11 atomics with explicit memory orders, C11 threads.h and POSIX pthread (mutex, condvar, rwlock, barrier), cache-line alignment to kill false sharing, ThreadSanitizer for race detection, and async-signal-safe rules for signal handlers. Use when designing multi-threaded data structures, debugging races/deadlocks, picking a memory order, or reasoning about lock-free patterns. Triggers on "原子操作", "memory_order", "TSan", "data race", "死锁", "条件变量", "false sharing", "pthread_mutex", "atomic_compare_exchange".
npx skillsauth add lazygophers/ccplugin c-concurrencyInstall this skill globally with one command. Works with Claude Code, Cursor, and Windsurf.
3 of 9 scanners reported clean
Some scanners were skipped, did not run, or reported a non-clean status. Review each row below.
_Atomic,要么用锁保护,不能"看起来安全"。memory_order_seq_cst;只有明确分析过 happens-before 后才放宽。while (!pred) 循环中等待,防止虚假唤醒。destroy。c-posix 中 POSIX.1 §2.4 列表),共享变量类型为 volatile sig_atomic_t 或 atomic_*。_Alignas(64))以消除 false sharing。<stdatomic.h>)_Atomic int counter = 0; // 关键字
atomic_int counter2 = 0; // typedef 别名
atomic_store(&counter, 42);
int v = atomic_load(&counter);
int old = atomic_fetch_add(&counter, 1);
// CAS
int expected = 0;
while (!atomic_compare_exchange_weak(&counter, &expected, expected + 1)) { }
| order | 用途 | 备注 |
|-------|-----|------|
| relaxed | 纯计数器 / 统计 | 无 happens-before |
| acquire | 读端(load) | 与同变量的 release 配对 |
| release | 写端(store) | 发布数据 |
| acq_rel | RMW | 同时具备两端语义 |
| seq_cst | 默认 | 全序,无脑安全 |
经典 publish-subscribe:
atomic_store_explicit(&ready, 1, memory_order_release); // 发布
if (atomic_load_explicit(&ready, memory_order_acquire)) // 读端
consume(data);
<threads.h>)int worker(void *arg) { /* ... */ return 0; }
thrd_t t;
if (thrd_create(&t, worker, &arg) != thrd_success) return -1;
int rc; thrd_join(t, &rc);
注:MSVC 仍未原生提供 <threads.h>;跨平台首选 pthread + _Atomic。
pthread_mutex_t m = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t c = PTHREAD_COND_INITIALIZER;
bool ready = false;
// 生产者
pthread_mutex_lock(&m);
ready = true;
pthread_cond_signal(&c); // 多消费者改用 broadcast
pthread_mutex_unlock(&m);
// 消费者
pthread_mutex_lock(&m);
while (!ready) pthread_cond_wait(&c, &m); // while,不是 if
pthread_mutex_unlock(&m);
pthread_mutex_destroy(&m);
pthread_cond_destroy(&c);
读写锁:pthread_rwlock_rdlock / wrlock / unlock / destroy。
_Alignas(64) struct PerCpu {
_Atomic uint64_t counter;
char _pad[64 - sizeof(_Atomic uint64_t)];
};
gcc -std=c17 -fsanitize=thread -g -O1 prog.c -lpthread -o prog
./prog # 自动报告 data race / lock-order-inversion / deadlock
TSan 开销 5–15×。CI 中跑全量测试用例可显著降低生产环境竞态风险。
允许:write, _exit, sigaction, kill, read, sem_post(部分平台),原子 store/load。
禁止:malloc/free, printf/snprintf, pthread_mutex_*(除 pthread_kill 投递路径)。
信号 ↔ 主流程通信使用 volatile sig_atomic_t 或 atomic_int。
acquire/release 配对足够。load。liburcu、folly、boost.lockfree 等成熟实现,自写需充分 stress test。_Atomic 或锁保护while 循环中等待development
Go 数据库规范——GORM Model 命名 ModelXxx、表名单数、枚举 uint8 + 常量、索引 idx_ 前缀 + deleted_at leading column、禁 time.Time 统一 int64 unix、禁指针/nullable 字段、TEXT/BLOB/JSON 禁 default、AutoMigrate 禁改主键。设计 DB model、写 GORM tag、建索引、做 migration 审查时触发。
development
Go HTTP API 规范——响应始终 200 + body code 字段、路由 /api/* 全 POST 单段 <Action><Model>、中间件逐路由注册禁 Group(prefix,mw...)、handler 仅返回 (rsp,error)、认证走 header。设计 HTTP API、写路由/handler/中间件时触发。
development
Go 项目结构规范——三层架构(API → Impl → State)、全局状态模式、internal/ 私有包、cmd/ 仅 main.go、go.work 多模块、禁止 Repository 接口和 DI 容器、struct 公共字段开头全 omitempty、handler var rsp 顶声明、禁 legacy migration。设计项目骨架、新建目录、组织包、做架构评审时触发。
development
Go 命名规范——Id/Uid 字段(非 ID)、IsActive/HasMFA 布尔前缀、CreatedAt 时间字段、接收者统一用 p、包名全小写无下划线、泛型类型参数描述性命名、集合字段 xxx_list 禁 xxxs 复数、Enum 0 值 XxxNil 禁 Unknown、禁 Status 统一 State、Set/Update 语义区分。定义结构体字段、函数、变量、包、接收者名、泛型、枚举时触发。