0

I'm new to c++ compiling, tooling, llvm and such. I'm exploring ways of compiling some c++ apps for the browser. I'm not looking for solutions that just run the c++ app. For those situation emscripten seems to be just right. I'm looking for ways to build a hybrid app that has a lot of touch-points between the javascript part and the c++ part.

I had success compiling and running some c/c++ apps using the wasi-sdk provided clang and llvm. But the llvm provided by wasi-sdk does not support threads.

The wasi-sdk offers a set of stdlib that respect the wasi specification. This specification does not support multi-threading. Is there a way to add the pthreads from other stdlib implementations and implement the javascript glue code by hand (maybe seeking inspiration from emscripten). If yes, what would be the steps? LLVM seems to be compiled without threads support in wasi-sdk, so simply adding additional headers that define pthreads might not work.

Vlad Nicula
  • 3,577
  • 6
  • 32
  • 50
  • Seems like you are looking for WebASM [Emscripten pthreads](https://emscripten.org/docs/porting/pthreads.html) – Victor Gubin Oct 14 '22 at 20:21
  • I did look a those. Yes. However the code generated by the Emscripten is huge and not open for extension. I'm looking for a way to just get the binary, without glue code, and with WASI functions which are easier to manage and documented, while Emscripten ones seem to be undocumented and cryptic (like syscall200 instaed of printf or whatever) – Vlad Nicula Oct 14 '22 at 20:35

1 Answers1

0

Wasi (and wasi-sdk and wasi-libc) doesn't currently support threads. There is an effort underway to add support here: https://github.com/WebAssembly/wasi-threads

There have been several recent patches to wasi-libc: e.g. https://github.com/WebAssembly/wasi-libc/pull/325.

sbc100
  • 2,619
  • 14
  • 11
  • Oh you shared a committed pthread? Thanks, I'll take a look. Any pointers on how to start using threads in the browser now? Trying to compile with clang and llvm provided by wasi won't work because llvm was compiled without threads support (or at least this is my understanding of things) – Vlad Nicula Oct 15 '22 at 03:27
  • To use threads in the browser today you would need to build using emscripten. It has support for pthreads via WebWorkers. – sbc100 Oct 17 '22 at 16:55
  • I thought that if pthreads work with emscripten, it means, they can work without emscripten, by just writing a similar system in javascript and compiling a certain way the C++ code. – Vlad Nicula Oct 24 '22 at 11:44
  • I guess emscripten is a magic blackbox and the information on how to build a similar system is not easily accessible. – Vlad Nicula Oct 24 '22 at 11:44