Passaggio di messaggi e canali tra thread WASM
Coordini worker WASM paralleli usando pattern di passaggio dei messaggi e astrazioni di canale invece della memoria condivisa non astratta.
Passaggio di messaggi e canali tra thread WASM è una lezione WebAssembly (WASM) for High Performance Apps gratuita su CoddyKit. Questa è la lezione 4 di 4. Puoi leggere la lezione completa qui gratuitamente — poi esercitati direttamente nel browser con un editor di codice integrato e un tutor IA disponibile 24/7. Fa parte del percorso di apprendimento WebAssembly (WASM) for High Performance Apps, e i tuoi progressi si sincronizzano tra il web e l'app CoddyKit. Il corso WebAssembly (WASM) for High Performance Apps include 4 lezioni in totale.
Parti di questa lezione non sono ancora state tradotte e vengono mostrate in inglese.
Two Models of Concurrency
Concurrent systems coordinate either by sharing memory (SharedArrayBuffer + atomics) or by passing messages. Message passing avoids many data races by design.
postMessage Basics
Web Workers communicate with postMessage. Data is structured-cloned, so each side gets its own copy.
const worker = new Worker("worker.js");
worker.postMessage({ cmd: "start", n: 1000 });
worker.onmessage = (e) => console.log("result", e.data);Transferable Objects
To avoid copying large buffers, transfer ownership instead of cloning.
const buf = new ArrayBuffer(1024 * 1024);
worker.postMessage({ buf }, [buf]);
// buf is now neutered in the senderDesigning a Channel
A channel is a typed queue abstraction layered over postMessage: a send side and a receive side with backpressure and request IDs.
Request/Response Correlation
Attach a unique id to each message so async replies can be matched to their originating request via a pending-promise map.
const pending = new Map();
let nextId = 1;
function call(payload) {
const id = nextId++;
return new Promise((res) => {
pending.set(id, res);
worker.postMessage({ id, payload });
});
}Fan-Out Work Pools
For data parallelism, spawn N workers and round-robin tasks to them. Collect results as they arrive, preserving order with the request IDs.
Combining with Shared Memory
Message passing for control, SharedArrayBuffer for bulk data is a powerful hybrid: send a small message saying which slice to process, while the data lives in shared memory.
Avoiding Deadlocks
Pure message-passing systems still deadlock if two parties each wait for the other. Keep protocols acyclic and prefer non-blocking sends.
Serialization Cost
Structured clone is convenient but costly for big graphs. Prefer transferables or shared memory when payloads grow large.
Worker Lifecycle
Create workers up front in a pool and reuse them; spinning up a worker per task wastes time loading and instantiating the WASM module each time.
A Channel-Based Pipeline
Chain stages: source worker emits chunks, a transform worker processes them, a sink worker aggregates — each connected by a channel for a clean streaming pipeline.
Quick Check
How do you send a large ArrayBuffer to a worker without copying it?
Recap
You explored message passing with postMessage, transferables to avoid copies, and channel abstractions with request IDs and worker pools. Hybrid designs pair messages for control with shared memory for bulk data.
Impara WebAssembly (WASM) for High Performance Apps con un tutor IA — gratis
Scrivi ed esegui vero codice nel tuo browser, ricevi aiuto istantaneo da un tutor IA disponibile 24/7, e riprendi da dove hai lasciato sul web o nell'app.
- Corsi
- 12
- Lezioni
- 48
Domande Frequenti
La lezione «Passaggio di messaggi e canali tra thread WASM» è gratuita?
Sì — il testo completo di «Passaggio di messaggi e canali tra thread WASM» è gratuito qui sul web. Per esercitarvi in modo interattivo (un editor di codice integrato e un tutor IA 24/7) e sbloccare il resto del corso WebAssembly (WASM) for High Performance Apps, passa a CoddyKit PRO. Il corso WebAssembly (WASM) for High Performance Apps include 4 lezioni in totale.
Cosa imparerò in «Passaggio di messaggi e canali tra thread WASM»?
Coordini worker WASM paralleli usando pattern di passaggio dei messaggi e astrazioni di canale invece della memoria condivisa non astratta. Eserciti WebAssembly (WASM) for High Performance Apps con codice pratico che esegui direttamente nel browser, e un tutor IA 24/7 risponde alle tue domande mentre lavori sulla lezione.
Ho bisogno di esperienza per iniziare WebAssembly (WASM) for High Performance Apps?
Non è richiesta alcuna esperienza precedente. WebAssembly (WASM) for High Performance Apps su CoddyKit è strutturato per principianti e studenti avanzati, quindi puoi iniziare da qui o dall'inizio e procedere al tuo ritmo. Questa è la lezione 4 di 4.
Quanto tempo richiede la lezione «Passaggio di messaggi e canali tra thread WASM»?
La maggior parte delle lezioni CoddyKit richiede circa 5–10 minuti. Ogni lezione è breve e interattiva, quindi fai progressi costanti e riprendi esattamente da dove hai lasciato su web e app.
Posso scrivere ed eseguire codice in questa lezione WebAssembly (WASM) for High Performance Apps?
Sì. Ogni lezione WebAssembly (WASM) for High Performance Apps include un editor di codice integrato, quindi scrivi ed esegui codice reale direttamente nel tuo browser e ricevi feedback istantaneo dall'IA — nessuna configurazione locale necessaria.
Tutte le lezioni di questo corso
- Web Worker con thread WASM
- SharedArrayBuffer e atomiche per WASM
- Progettare applicazioni WASM concorrenti
- Passaggio di messaggi e canali tra thread WASM