Install any skill in seconds. Free to start, no credit card required.
Get Started Free →Use when running work off the main thread — WorkerThreadPool, Thread/Mutex/Semaphore, call_deferred, thread-safe scene access, and threaded resource loading
.claude/skills/jame581-multithreading/SKILL.md| Test case | Without → With | Effect | Δ tokens | Δ turns |
|---|---|---|---|---|
| case-05 | ✗→✓ | ▲ Improved | 80% | 0% |
| case-06 | ✗→✓ | ▲ Improved | 109% | 0% |
| case-14 | ✗→✓ | ▲ Improved | 63% | 0% |
| case-08 | ✗→✓ | ▲ Improved | 166% | 0% |
| case-10 | ✗→✓ | ▲ Improved | 52% | 0% |
Run expensive work off the main thread without corrupting the scene tree. Prefer WorkerThreadPool for short parallel jobs; reach for Thread/Mutex/Semaphore only when you need a long-lived worker.
> Related skills: godot-optimization for profiling before threading, assets-pipeline for asset import, csharp-godot for C# specifics, gdscript-advanced for async/await pitfalls.
The main thread owns the scene tree — interacting with the active scene tree is not thread-safe. Observe these doc-sourced rules:
Rendering > Driver > Thread Model = Separate, Physics > {2D,3D} > Run on Separate Thread). Servers handle thousands of thread-driven instances well.Navigation > Pathfinding > Max Threads.Array/Dictionary: reading/writing existing elements across threads is OK; resizing (add/remove) needs a Mutex.add_child.call_deferred() — only with a single loader thread (multiple threads risk tweaking the same cached resource → crashes).> Golden rule: Mutate the scene tree only on the main thread. From a worker, hand results back with call_deferred / set_deferred.
WorkerThreadPool is a global singleton with threads allocated at startup. A regular task (add_task) runs on one worker; a group task (add_group_task) is distributed across workers, calling the Callable repeatedly for each element index — great for iterating many elements. Every task must be waited on (wait_for_task_completion / wait_for_group_task_completion) or its allocated resources leak. Distributing cheap work can hurt performance — only use it for genuinely expensive work.
gdscriptvar enemies = [] # Filled with enemies elsewhere. func process_enemy_ai(enemy_index): var processed_enemy = enemies[enemy_index] # Expensive per-enemy logic... func _process(delta): var task_id = WorkerThreadPool.add_group_task(process_enemy_ai, enemies.size()) # ... other main-thread work ... WorkerThreadPool.wait_for_group_task_completion(task_id) # Safe to read results now.
csharpprivate List<Node> _enemies = new(); // Filled with enemies elsewhere. private void ProcessEnemyAI(int enemyIndex) { Node processedEnemy = _enemies[enemyIndex]; // Expensive per-enemy logic... } public override void _Process(double delta) { long taskId = WorkerThreadPool.AddGroupTask(Callable.From<int>(ProcessEnemyAI), _enemies.Count); // ... other main-thread work ... WorkerThreadPool.WaitForGroupTaskCompletion(taskId); // Safe to read results now. }
This relies on the element count staying constant during the multithreaded part.
Real signatures: Thread.start(callable: Callable, priority := PRIORITY_NORMAL), wait_to_finish() (blocks; join before free), is_alive(). Mutex is reentrant (lock/unlock/try_lock). Semaphore exposes wait() / post(count := 1).
The canonical semaphore producer/consumer + clean-shutdown idiom:
gdscriptvar counter := 0 var mutex: Mutex var semaphore: Semaphore var thread: Thread var exit_thread := false func _ready(): mutex = Mutex.new() semaphore = Semaphore.new() thread = Thread.new() thread.start(_thread_function) func _thread_function(): while true: semaphore.wait() # Block until there is work. mutex.lock() var should_exit = exit_thread mutex.unlock() if should_exit: break mutex.lock() counter += 1 mutex.unlock() func increment_counter(): semaphore.post() # Wake the worker. func _exit_tree(): mutex.lock() exit_thread = true mutex.unlock() semaphore.post() # Unblock so it can see exit_thread. thread.wait_to_finish() # Join.
Godot.Mutex/Godot.Semaphore also exist, but System.Threading is idiomatic in C#:
csharpusing Godot; using System.Threading; public partial class Worker : Node { private int _counter; private readonly object _lock = new(); private readonly SemaphoreSlim _semaphore = new(0); private Thread _thread; private volatile bool _exitThread; public override void _Ready() { _thread = new Thread(ThreadFunction) { IsBackground = true }; _thread.Start(); } private void ThreadFunction() { while (true) { _semaphore.Wait(); // Block until there is work. if (_exitThread) break; lock (_lock) { _counter++; } } } public void IncrementCounter() => _semaphore.Release(); // Wake the worker. public override void _ExitTree() { _exitThread = true; _semaphore.Release(); // Unblock so it can see _exitThread. _thread.Join(); // Join. } }
Thread creation is slow (especially on Windows) — pre-create before heavy work, not just-in-time. Over-locking mutexes is also costly.
gdscript# Unsafe from a worker thread: world.add_child(enemy) # Safe: world.add_child.call_deferred(enemy)
csharp// Unsafe from a worker thread: world.AddChild(enemy); // Safe — use the MethodName StringName constant, NOT "AddChild": world.CallDeferred(Node.MethodName.AddChild, enemy);
In C#, CallDeferred("AddChild") fails — the deferred/Call/Connect APIs use Godot's snake_case names. Prefer the Node.MethodName.* constants (avoids the pitfall and an allocation).
ResourceLoader.load_threaded_request(path) starts the load. Poll load_threaded_get_status(path, progress) each frame (progress[0] is the 0–1 ratio); on THREAD_LOAD_LOADED call load_threaded_get(path). load_threaded_get blocks like load() if the load is not finished — always poll first. Statuses: THREAD_LOAD_INVALID_RESOURCE / THREAD_LOAD_IN_PROGRESS / THREAD_LOAD_FAILED / THREAD_LOAD_LOADED.
gdscriptconst SCENE_PATH := "res://enemy.tscn" var _progress: Array = [] func _ready(): ResourceLoader.load_threaded_request(SCENE_PATH) func _process(_delta): var status := ResourceLoader.load_threaded_get_status(SCENE_PATH, _progress) match status: ResourceLoader.THREAD_LOAD_IN_PROGRESS: $ProgressBar.value = _progress[0] * 100.0 ResourceLoader.THREAD_LOAD_LOADED: var scene: PackedScene = ResourceLoader.load_threaded_get(SCENE_PATH) add_child(scene.instantiate()) set_process(false) ResourceLoader.THREAD_LOAD_FAILED, ResourceLoader.THREAD_LOAD_INVALID_RESOURCE: push_error("Threaded load failed: %s" % SCENE_PATH) set_process(false)
csharpprivate const string ScenePath = "res://enemy.tscn"; private readonly Godot.Collections.Array _progress = new(); public override void _Ready() => ResourceLoader.LoadThreadedRequest(ScenePath); public override void _Process(double delta) { var status = ResourceLoader.LoadThreadedGetStatus(ScenePath, _progress); switch (status) { case ResourceLoader.ThreadLoadStatus.InProgress: GetNode<ProgressBar>("ProgressBar").Value = (double)_progress[0] * 100.0; break; case ResourceLoader.ThreadLoadStatus.Loaded: var scene = (PackedScene)ResourceLoader.LoadThreadedGet(ScenePath); AddChild(scene.Instantiate()); SetProcess(false); break; case ResourceLoader.ThreadLoadStatus.Failed: case ResourceLoader.ThreadLoadStatus.InvalidResource: GD.PushError($"Threaded load failed: {ScenePath}"); SetProcess(false); break; } }
> Godot 4.7+: 4.7 shipped several threaded-load correctness fixes — load_threaded_get() deadlocks (GH-119757, GH-120077), a race in load_threaded_request() (GH-118824), and resources returned by load_threaded_get() never being unloaded (GH-119394). No API change; if you carry workarounds for rare threaded-load hangs or leaks from earlier versions, re-test on 4.7 before keeping them. The poll-before-get rule above still applies.
In C#, prefer System.Threading.Tasks.Task.Run / async-await for fire-and-forget CPU work; never touch Godot objects or await ToSignal(...) from a background thread — marshal results back with CallDeferred. Use WorkerThreadPool when you want Godot's pool and engine integration; use Task when you want .NET idioms. (GDScript users: use WorkerThreadPool or Thread from the sections above.)
csharppublic override void _Process(double delta) { if (Input.IsActionJustPressed("compute")) { _ = System.Threading.Tasks.Task.Run(() => { int result = ExpensiveComputation(); // Pure CPU work, no Godot objects. CallDeferred(MethodName.OnComputed, result); // Marshal back to main thread. }); } } private void OnComputed(int result) => GD.Print($"Done: {result}");
> Deeper: see Pitfalls & deadlocks for data races, the ERR_BUSY nested-wait deadlock, and when threading hurts.
call_deferred / set_deferred)WorkerThreadPool task is waited on (wait_for_*_completion)Mutex / lock; container resizes are lockedwait_to_finish / Join) before the owning node freesload_threaded_get| Case | Status | Duration (ms) | Turns | Tokens | Tool calls | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Without | With | Δ | Without | With | Δ | Without | With | Δ | Without | With | Δ | ||
case-01 | pass→pass | 12,447 | 7,694 | -38% | 1 | 1 | 0% | 2,168 | 4,213 | +94% | 0 | 0 | — |
case-02 | pass→pass | 10,728 | 4,001 | -63% | 1 | 1 | 0% | 1,761 | 3,564 | +102% | 0 | 0 | — |
case-03 | pass→pass | 11,404 | 8,510 | -25% | 1 | 1 | 0% | 1,828 | 4,379 | +140% | 0 | 0 | — |
case-13 | pass→pass | 11,351 | 9,411 | -17% | 1 | 1 | 0% | 1,970 | 4,443 | +126% | 0 | 0 | — |
case-04 | pass→pass | 8,868 | 7,646 | -14% | 1 | 1 | 0% | 1,617 | 4,352 | +169% | 0 | 0 | — |
case-05 | fail→pass | 14,588 | 7,168 | -51% | 1 | 1 | 0% | 2,286 | 4,118 | +80% | 0 | 0 | — |
case-06 | fail→pass | 15,045 | 10,507 | -30% | 1 | 1 | 0% | 2,314 | 4,833 | +109% | 0 | 0 | — |
case-07 | pass→pass | 16,766 | 14,032 | -16% | 1 | 1 | 0% | 2,668 | 5,261 | +97% | 0 | 0 | — |
case-14 | fail→pass | 17,414 | 9,050 | -48% | 1 | 1 | 0% | 2,616 | 4,262 | +63% | 0 | 0 | — |
case-08 | fail→pass | 8,397 | 4,281 | -49% | 1 | 1 | 0% | 1,355 | 3,601 | +166% | 0 | 0 | — |
case-09 | pass→pass | 13,478 | 7,920 | -41% | 1 | 1 | 0% | 2,175 | 4,250 | +95% | 0 | 0 | — |
case-10 | fail→pass | 17,374 | 7,645 | -56% | 1 | 1 | 0% | 2,663 | 4,050 | +52% | 0 | 0 | — |
case-11 | pass→pass | 15,507 | 7,275 | -53% | 1 | 1 | 0% | 2,483 | 4,150 | +67% | 0 | 0 | — |
case-12 | fail→pass | 12,867 | 9,287 | -28% | 1 | 1 | 0% | 1,766 | 4,196 | +138% | 0 | 0 | — |
case-15 | fail→pass | 8,032 | 5,911 | -26% | 1 | 1 | 0% | 1,330 | 3,870 | +191% | 0 | 0 | — |
case-16 | pass→pass | 4,355 | 2,557 | -41% | 1 | 1 | 0% | 765 | 3,319 | +334% | 0 | 0 | — |
case-17 | fail→fail | 13,159 | 5,811 | -56% | 1 | 1 | 0% | 1,968 | 3,888 | +98% | 0 | 0 | — |
case-18 | pass→pass | 7,943 | 5,192 | -35% | 1 | 1 | 0% | 1,432 | 3,835 | +168% | 0 | 0 | — |
case-19 | pass→pass | 8,321 | 7,654 | -8% | 1 | 1 | 0% | 1,353 | 4,062 | +200% | 0 | 0 | — |
case-20 | pass→pass | 7,750 | 5,208 | -33% | 1 | 1 | 0% | 1,208 | 3,712 | +207% | 0 | 0 | — |
case-21 | fail→pass | 9,069 | 10,867 | +20% | 1 | 1 | 0% | 1,394 | 4,517 | +224% | 0 | 0 | — |
case-22 | pass→pass | 9,907 | 8,934 | -10% | 1 | 1 | 0% | 1,595 | 4,317 | +171% | 0 | 0 | — |
case-23 | pass→fail | 7,857 | 7,534 | -4% | 1 | 1 | 0% | 1,281 | 4,097 | +220% | 0 | 0 | — |
DecimalAI ran this skill against gemini-3.6-flash twice over the same eval suite — once with the skill loaded and once without — and compared the two runs case by case. 23 cases were attempted. The headline lift of +30 percentage points is the difference between those two pass rates over the 23 comparable cases. 1 case got worse with the skill loaded, and it is included in that figure.
Without the skill loaded, the model failed this case. With it loaded, the same prompt on the same model passed. This is one improved case from the latest verified run; every case, including any that regressed, is in the table above.
Other measured skills in the registry, with their headline benchmark lift.