Demo project showing how to use WebAssembly and Web Workers.
You need emscripten to compile the WebAssembly sources using compile-cpp.bat
(or just see the exact command for non-Windows).
Next, you will need following fixes to build using shared memory and mutex:
- Copy the contents of
mutex.wast
into the generatedworker.wast
file just above the line(export "_colorCells" (func $_colorCells))
- Delete the two mutex imports
__ZNSt3__25mutex4lockEv
and__ZNSt3__25mutex4lockEv
as we have provided implementation for them. - Change memory import to
(import "env" "memory" (memory $memory 256 256 shared))
(addshared
at the end)
You need to compile the updated worker.wast
file back to .wasm
. You can do this using the wat2wasm
tool from WebAssembly Binary Toolkit (with --enable-threads
option) or using this web demo (with the threads checkbox checked). For the former, check compile-wast.bat
.
You can play around with USE_MUTEX
in worker.cpp
to see how locking affects multiple workers.
Configure any Web server to point to the build
folder.
For now, only Chrome supports SharedArrayBuffer
(and hence shared WebAssembly memory).