Skip to content

Armchair-Software/emscripten-sync

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 

Repository files navigation

Emscripten WASM Worker C++ Synchronisation Primitives

Header-only C++ library offering drop-in replacements for std::mutex, using Emscripten's WASM Worker API.

Why?

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.

How?

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};

Contributions welcome

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_semaphore equivalent, using emscripten_semaphore_release and emscripten_semaphore_waitinf_acquire / emscripten_semaphore_try_acquire.
    • TODO: implement std::binary_semaphore equivalent using the above.
  • TODO: implement std::condition_variable equivalent, using emscripten_condvar_wait, emscripten_condvar_waitinf and emscripten_condvar_signal.

Other useful libraries

You may also find the following Emscripten helper libraries useful:

About

C++ Standard-library compatible synchronisation primitives, implemented for Emscripten WASM Workers

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages