Header-only C++ library offering drop-in replacements for std::mutex, using Emscripten's WASM Worker API.
You may wish to build multithreaded programs for Emscripten using WASM Workers, without compiling with -pthread. In this case, std::mutex will not work (see emscripten-core/emscripten#23661).
In this situation, emscripten_sync::mutex can be used as a drop-in replacement.
Even if you have -pthread enabled, emscripten-sync::mutex may offer better performance than std::mutex when used with WASM workers, as it may have lower overhead. Benchmark both approaches to be sure you're choosing the best for your application.
Just use emscripten_sync::mutex wherever you'd use std::mutex explicitly, or where a template parameter defaults to std::mutex.
For example,
std::mutex mutex;
std::scoped_lock lock{mutex};can be replaced with
emscripten_sync::mutex mutex;
std::scoped_lock lock{mutex};At the time of writing, only mutex is implemented. Other synchronisation primitives could also be implemented using Emscripten's WASM worker API:
- TODO: implement
std::counting_semaphoreequivalent, usingemscripten_semaphore_releaseandemscripten_semaphore_waitinf_acquire/emscripten_semaphore_try_acquire.- TODO: implement
std::binary_semaphoreequivalent using the above.
- TODO: implement
- TODO: implement
std::condition_variableequivalent, usingemscripten_condvar_wait,emscripten_condvar_waitinfandemscripten_condvar_signal.
You may also find the following Emscripten helper libraries useful:
- Emscripten Browser File Library - allows you to transfer files using the browser upload / download interface, into memory in your C++ program.
- Emscripten Browser Clipboard Library - easy handling of browser copy and paste events in your C++ code.
- Emscripten Browser Cursor - easy manipulation of browser cursors, for Emscripten applications.
- Emscripten Audio - Lightweight C++ library to make it easy to work with realtime low-latency WASM audio worklets under Emscripten.