شعار Backend Concurrency Mechanics

Backend Concurrency Mechanics

المجتمع
j4flmao
backend-concurrency-mechanics

Use this skill when the user says 'race condition', 'data race', 'goroutine vs thread', 'event loop blocking', 'memory barrier', 'memory model', 'mutex vs atomic', 'deadlock', 'thread pool sizing', 'virtual threads', 'async vs threads'. Explains how OS threads, M:N schedulers (Go goroutines, Java virtual threads), and event loops (Node.js, Python asyncio) actually execute work, and gives correct synchronization patterns for backend services. Do NOT use for: cross-process locks (backend-distributed-locking), job queues (backend-background-jobs), database isolation levels (backend-database-patterns).

نظرة عامة

الناشرj4flmao
المستودعagent-skills
اسم المهارةbackend-concurrency-mechanics
النجوم
41
التفريعات
6
الملفات المرفقة
تعليمات فقط
الترخيصMIT
الروابط
  • تعليمات بصيغة Markdown

    ملف SKILL.md يُحمّله النموذج عند الحاجة، فلا يستهلك توكنات إلا عندما يَرِد طلب ذو صلة فعلًا.

  • تعمل مع أي نموذج لغوي كبير

    مهارات الذكاء الاصطناعي مكتوبة بصيغة Markdown وليست شيفرة خاصة بمزوّد معيّن، لذا تعمل مع GPT أو Claude أو Gemini أو Grok أو نموذج محلي.

  • مكتفية بذاتها

    كل ما يحتاجه النموذج موجود في التعليمات — لا ملفات إضافية لمزامنتها.

  • مفتوحة المصدر

    نشرها j4flmao على GitHub. اطّلع على المصدر قبل تثبيتها.

التثبيت

ثبّت مهارة الذكاء الاصطناعي Backend Concurrency Mechanics في TypingMind لاستخدامها مع أي نموذج لغوي كبير، أو أضفها إلى وكيل ذكاء اصطناعي آخر يقرأ SKILL.md.

1

التثبيت في TypingMind

يُثبّت TypingMind المهارة مباشرةً من مجلدها على GitHub — فيقرأ SKILL.md، ويجمع ملفات الموارد في حزمة، ويحفظ النتيجة محليًا.

  1. افتح التطبيق وانتقل إلى «الإضافات» ثم «المهارات».
  2. اختر «تثبيت من GitHub».
  3. الصق رابط مجلد المهارة الموضّح أدناه وأكّد التثبيت.
    https://github.com/j4flmao/agent-skills/tree/main/skills/backend/universal/concurrency
  4. فعّل المهارة في كل محادثة تريد إتاحتها فيها.
الإضافات ← المهارات ← إضافة مهارة ← من رابط GitHub، ثم الصق رابط المجلد واضغط على «متابعة».
2

التثبيت في وكيل ذكاء اصطناعي آخر

يمكن لأي وكيل ذكاء اصطناعي يقرأ تنسيق Agent Skills استخدام هذه المهارة — انسخ المجلد إلى مجلد المهارات الخاص بذلك الوكيل.

Claude Code — .claude/skills
git clone --depth 1 https://github.com/j4flmao/agent-skills.git /tmp/agent-skills
mkdir -p .claude/skills
cp -r /tmp/agent-skills/skills/backend/universal/concurrency .claude/skills/backend-concurrency-mechanics
أعد تشغيل Claude Code بعد النسخ ليتعرّف على المهارة الجديدة.

استخدمها في TypingMind

فعّل Backend Concurrency Mechanics في أي محادثة على TypingMind وسيتولى النموذج الباقي. تتضمن مطالبة النظام اسمها ووصفها، وبمجرد ورود طلب ذي صلة، يُحمّل النموذج التعليمات الكاملة تلقائيًا — فلا تحتاج إلى استدعائها يدويًا، ولا تستهلك أي توكنات حتى تُستخدم فعليًا.

يُحمّل النموذج مهارة Backend Concurrency Mechanics تلقائيًا بمجرد ورود طلب ذي صلة بها.

تعمل مع أي نموذج ذكاء اصطناعي

مهارات الذكاء الاصطناعي هي تعليمات بصيغة Markdown وليست شيفرة خاصة بمزوّد معيّن، لذا لا ترتبط Backend Concurrency Mechanics بالنموذج الذي كُتبت له. ثبّتها مرة واحدة في TypingMind واستخدمها مع GPT-5 أو Claude أو Gemini أو Grok أو DeepSeek أو Mistral أو Llama أو نموذج محلي تشغّله بنفسك — كل ذلك باستخدام مفاتيح API الخاصة بك.

  • تُحمَّل عند الحاجة فقط

    تتضمن مطالبة النظام الاسم والوصف فقط. وتُجلب التعليمات عند أول طلب ذي صلة، لذا لا تستهلك تعليمات المهارة أي توكنات ما دامت المهارة غير مستخدمة.

  • بدّل النماذج أثناء المحادثة

    لأن المهارة تعليمات وليست شيفرة، فإن تغيير النموذج لا يعطّلها — فالنموذج التالي يقرأ ملف SKILL.md نفسه.

تعليمات المهارة

هذا هو محتوى SKILL.md الذي يُحمّله النموذج. اقرأه قبل التثبيت — فالمهارة عبارة عن تعليمات سيتبعها نموذجك.

Concurrency Mechanics

Purpose

Pick the right concurrency model for a workload and write shared-state code that is correct under the language's memory model. Most production concurrency bugs come from unsynchronized shared state, blocking an event loop, or unbounded parallelism; this skill targets those.

Agent Protocol

Trigger

Exact user phrases: "race condition", "data race", "goroutine vs thread", "event loop blocking", "memory barrier", "memory model", "mutex vs atomic", "deadlock", "thread pool sizing", "virtual threads", "async vs threads".

Input Context

  • Language/runtime and version (Go, Java 21+, Node.js, Python, Rust, .NET).
  • Workload type: I/O-bound, CPU-bound, or mixed.
  • The shared state or code path that misbehaves; any race detector output.

Output Artifact

Explanation of the execution model plus corrected code. No files unless requested.

Response Format

Model: {OS threads | M:N (goroutines/virtual threads) | event loop}
Workload: {I/O-bound | CPU-bound}
Shared state: {what} -> {mutex | atomic | channel/actor | immutable copy}
Bound: {pool size / semaphore N}
Verify: {go test -race | jcstress | stress test}

Execution Models

ModelExamplesSchedulingCost per unitBlocking call behaviour
1:1 OS threadsJava platform threads, C++ std::thread, Python threadsKernel schedulerEach thread reserves a stack (commonly 1 MB on Windows, 8 MB virtual on Linux glibc, committed lazily) plus kernel structuresBlocks only that thread
M:N user-spaceGo goroutines, Java 21 virtual threads, Erlang processesRuntime multiplexes many tasks onto a few OS threadsSmall growable stacks (Go starts at 2 KB)Runtime parks the task; blocking syscalls may pin a carrier thread
Event loopNode.js (libuv), Python asyncio, Netty, Redis command executionSingle thread runs callbacks/coroutines when I/O is ready (epoll, kqueue, IOCP)Very cheap per connectionAny CPU-heavy or synchronous call stalls every request

Go scheduler (G-M-P)

  • G goroutine, M OS thread, P logical processor holding a local run queue; GOMAXPROCS sets the number of Ps (defaults to CPU count; since Go 1.25 it also respects cgroup CPU limits on Linux).
  • An idle P steals half of another P's local queue (work stealing) and also polls the global queue and network poller.
  • When a goroutine enters a blocking syscall, its M is detached from the P so another M can keep running goroutines.

Java virtual threads (Java 21+)

  • Mounted on a small ForkJoinPool of carrier threads; blocking I/O unmounts them.
  • Pinning: in Java 21-23, blocking inside synchronized pins the carrier; JDK 24 (JEP 491) removed that for synchronized. Native calls still pin.
  • Do not pool virtual threads; limit concurrency to downstream resources with a Semaphore.

Node.js event loop

  • JavaScript runs on one thread; network I/O is non-blocking via the OS; file system, DNS lookup, crypto and zlib use the libuv thread pool (default 4, UV_THREADPOOL_SIZE).
  • Microtasks (resolved promises, queueMicrotask) drain fully after each macrotask, so a promise loop can starve I/O.
  • CPU-bound work goes to worker_threads or another service.

Memory Models in Practice

Compilers and CPUs reorder memory operations. x86 (TSO) only allows a later load to pass an earlier store; ARM and POWER allow much more reordering. You do not write fences by hand in application code: the language memory model guarantees visibility when you use its synchronization primitives.

LanguageHappens-before established by
Gochannel send/receive, sync.Mutex, sync.WaitGroup, sync/atomic (sequentially consistent)
Javasynchronized, volatile, java.util.concurrent locks/atomics, Thread.start/join
C++/Ruststd::atomic with acquire/release or seq_cst ordering, mutexes
JSSingle-threaded; SharedArrayBuffer + Atomics between workers

A data race is two concurrent accesses to the same memory, at least one a write, without happens-before ordering. In Go and C++ the result is undefined behaviour territory (torn reads, stale values); do not reason about "benign" races.

Correct Patterns (Go)

go
package counter

import (
	"context"
	"sync"
	"sync/atomic"

	"golang.org/x/sync/errgroup"
)

// Single integer: atomic is enough.
type Hits struct{ n atomic.Int64 }

func (h *Hits) Inc()         { h.n.Add(1) }
func (h *Hits) Load() int64  { return h.n.Load() }

// Compound invariant (map + total) needs a mutex: atomics cannot make
// two updates appear as one.
type Stats struct {
	mu     sync.Mutex
	byPath map[string]int
	total  int
}

func NewStats() *Stats { return &Stats{byPath: make(map[string]int)} }

func (s *Stats) Record(path string) {
	s.mu.Lock()
	defer s.mu.Unlock()
	s.byPath[path]++
	s.total++
}

// Bounded fan-out: never start one goroutine per item without a limit.
func FetchAll(ctx context.Context, urls []string, fetch func(context.Context, string) error) error {
	g, ctx := errgroup.WithContext(ctx)
	g.SetLimit(16) // bound concurrency to protect downstreams
	for _, u := range urls {
		g.Go(func() error { return fetch(ctx, u) }) // Go 1.22+: loop var is per-iteration
	}
	return g.Wait() // first error cancels ctx for the rest
}

Verify with go test -race ./... in CI; the race detector only finds races on executed paths, so run it with concurrent tests.

Choosing a Primitive

SituationUse
Single counter/flagAtomic
Several fields that must change togetherMutex (RWMutex only if reads dominate and critical sections are long enough to matter)
Ownership transfer / pipeline stagesChannels or queues
Read-mostly configImmutable snapshot swapped via atomic pointer (atomic.Pointer[T], AtomicReference)
Limit concurrent calls to a dependencySemaphore / bounded pool
Per-key serializationShard locks by hash of key, or an actor/queue per key

Pool Sizing

  • CPU-bound: about the number of cores; more threads only add context switches.
  • I/O-bound with threads: Little's law, concurrency = throughput x latency (e.g. 200 req/s x 0.05 s = 10 in flight), then cap at what the downstream (DB pool, API rate limit) accepts.
  • The DB connection pool is usually the real limit; size app concurrency to it, not the other way round.

Deadlocks and Liveness

  • Acquire multiple locks in one global order.
  • Never hold a lock while doing I/O or calling unknown callbacks.
  • Always pass deadlines (context.Context, CompletableFuture.orTimeout, AbortSignal.timeout).
  • Goroutine leaks: every goroutine needs an exit path (closed channel or cancelled context); monitor runtime.NumGoroutine().

Anti-Patterns

Anti-patternFix
sync.WaitGroup + shared slice append without lockPre-size slice and write by index, or collect via channel
JSON.parse of 50 MB or bcrypt sync calls in Node request pathStream/parse in worker thread; use async APIs
time.Sleep/polling for coordinationChannels, condition variables, or context
Check-then-act on shared map (if !exists { insert })Single critical section or LoadOrStore
Unbounded goroutines/threads per requesterrgroup limit, semaphore, worker pool

Checklist

  • Execution model and workload type identified.
  • Every shared mutable value has one documented synchronization mechanism.
  • Concurrency bounded per downstream dependency.
  • Event loop free of CPU-heavy or synchronous calls.
  • Race detector / stress tests in CI; goroutine or thread counts monitored.

References

  • Related skills: skills/backend/universal/distributed-locking/SKILL.md, skills/backend/universal/background-jobs/SKILL.md, skills/backend/universal/resilience-patterns/SKILL.md.

الأسئلة الشائعة

ماذا تفعل مهارة الذكاء الاصطناعي Backend Concurrency Mechanics؟

Use this skill when the user says 'race condition', 'data race', 'goroutine vs thread', 'event loop blocking', 'memory barrier', 'memory model', 'mutex vs atomic', 'deadlock', 'thread pool sizing', 'virtual threads', 'async vs threads'. Explains how OS threads, M:N schedulers (Go goroutines, Java virtual threads), and event loops (Node.js, Python asyncio) actually execute work, and gives correct synchronization patterns for backend services. Do NOT use for: cross-process locks (backend-distributed-locking), job queues (backend-background-jobs), database isolation levels (backend-database-pa...

لماذا تستخدم Backend Concurrency Mechanics على TypingMind؟

لأنك تثبّتها مرة واحدة وتستخدمها مع أي نموذج. مهارة Backend Concurrency Mechanics عبارة عن نص Markdown وليست شيفرة خاصة بمزود بعينه، لذا تعمل المهارة نفسها مع GPT-5 أو Claude أو Gemini أو Grok أو نموذج محلي، ويمكنك تبديل النموذج أثناء المحادثة دون تعطّل المهارة. يعمل TypingMind بمفاتيح API الخاصة بك، فتدفع للمزودين مباشرةً بدلًا من اشتراك لكل مقعد، وتبقى مهاراتك ومحادثاتك في مساحة التخزين الخاصة بك.

كيف أثبّت Backend Concurrency Mechanics في TypingMind؟

في TypingMind، افتح «الإضافات»، ثم «المهارات»، ثم اختر «التثبيت من GitHub» والصق https://github.com/j4flmao/agent-skills/tree/main/skills/backend/universal/concurrency. يقرأ TypingMind ملف SKILL.md الخاص بالمهارة ويثبّتها كمهارة يمكنك تفعيلها لكل محادثة على حدة.

ما نماذج الذكاء الاصطناعي التي يمكنها استخدام Backend Concurrency Mechanics؟

أي نموذج تربطه في TypingMind. مهارات الذكاء الاصطناعي عبارة عن تعليمات Markdown وليست شيفرة خاصة بمزود بعينه، لذا يمكن لكل من GPT وClaude وGemini وGrok والنماذج المحلية تحميل هذه المهارة عندما يتوافق الطلب معها.

كم نموذج ذكاء اصطناعي يمكنني استخدامه مع Backend Concurrency Mechanics؟

أي عدد تريده. ما دام النموذج يدعم المهارات، يمكنك استخدام Backend Concurrency Mechanics معه، سواء كان GPT أو Claude أو Gemini أو Grok أو DeepSeek أو Mistral أو Llama أو غيرها، وكل ذلك على TypingMind باستخدام مفاتيح API الخاصة بك.

هل مهارة الذكاء الاصطناعي Backend Concurrency Mechanics مجانية؟

نعم. نشرها j4flmao على GitHub بموجب ترخيص MIT. لا تدفع إلا لمزود الذكاء الاصطناعي الخاص بك مقابل التوكنات التي تستخدمها.

ما هي مهارات الذكاء الاصطناعي؟

مهارة الذكاء الاصطناعي حزمة تعليمات قابلة لإعادة الاستخدام تعلّم نموذج الذكاء الاصطناعي كيفية تنفيذ مهمة محددة. وتتبع تنسيق Agent Skills المفتوح: ملف SKILL.md يتضمن اسمًا ووصفًا، إلى جانب أي نصوص برمجية أو قوالب أو ملفات مرجعية قد يحتاج إليها النموذج. لا يقرأ النموذج التعليمات إلا عندما يتوافق طلبك مع المهارة، لذا لا تترتب على المهارة المثبتة أي تكلفة حتى تُستخدم.

كيف تختلف مهارات الذكاء الاصطناعي عن الإضافات أو موصلات MCP؟

تمنح الإضافة أو موصل MCP النموذج أدوات جديدة يمكنه استدعاؤها، وهي شيفرة تُنفَّذ في مكان ما وتُرجع نتيجة. أما مهارة الذكاء الاصطناعي فتزوّد النموذج بالمعرفة ومنهجية العمل: كيفية التعامل مع مهمة، والخطوات التي ينبغي اتباعها، وما ينبغي أن تكون عليه المخرجات الجيدة. المهارات عبارة عن نصوص Markdown، لذا لا تحتاج إلى خادم أو مفتاح API أو بيئة تشغيل، وتعمل مع أي نموذج.

عرض الكل
شعار openclaw

Claw Score

openclaw
مؤسسةشائع

دقّق مستندات بطاقة تقييم نضج OpenClaw أو حدّثها استناداً إلى تصنيف الجذر ودرجات النضج وعناصر أدلة QA، من دون استخدام بيانات discrawl الخاصة بالمشرفين أو تقارير جرد مضمنة في المستودع.

82.2K
391K
الملفات: 51
عرض
شعار openclaw

Clawsweeper

openclaw
مؤسسةشائع

استخدم هذا لكل أعمال ClawSweeper: تقارير فحص مشكلات/طلبات سحب OpenClaw، ومهام الإصلاح، وطلبات السحب لإصلاحات السحابة، وأوامر منشن المشرفين @clawsweeper، والإصلاح التلقائي/الدمج التلقائي الموثوق الذي راجعه ClawSweeper، ومراقبة GitHub Actions…

82.2K
391K
الملفات: 1
عرض
شعار openclaw

Control Ui E2e

openclaw
مؤسسةشائع

استخدم هذا عند تصميم واجهة OpenClaw Control UI الرسومية أو اختبارها أو إصلاحها أو توسيعها، بما في ذلك معارض اختبار الإجهاد للواجهة مع مدخلات الملاحظات، وفحوص Vitest + Playwright الشاملة، وتدفقات Gateway المحاكية…

82.2K
391K
الملفات: 1
عرض
شعار openclaw

Deslop

openclaw
مؤسسةشائع

تنظيف مقتصر على تغييرات الفرع من الحشو البرمجي المولّد بالذكاء الاصطناعي: أزِل حشو التعليقات، وفحوص التحوط الزائدة، وتمويه الأنواع، وانحراف الأسلوب من تغييرات الفرع الحالية قبل المراجعة التلقائية.

82.2K
391K
التعليمات
عرض

أنشئ مساحة عملك للذكاء الاصطناعي الآن

اشترك في نشرتنا الإخبارية لتصلك أخبار الميزات الجديدة والهدايا القادمة 👇