# Issue with Using pthread in WASM

**URL:** https://discourse.vtk.org/t/issue-with-using-pthread-in-wasm/15767
**Category:** vtk.wasm
**Created:** [June 27, 2025, 1:34pm UTC](https://discourse.vtk.org/t/issue-with-using-pthread-in-wasm/15767 "2025-06-27T13:34:18Z")
**Posts on this page:** 10
**Page:** 1

<div class="post-metadata">

### Author: ![blocky](https://discourse.vtk.org/user_avatar/discourse.vtk.org/blocky/32/9531_2.png) [@blocky](https://discourse.vtk.org/u/blocky)
#### Post date: [June 27, 2025, 1:34pm UTC](https://discourse.vtk.org/t/issue-with-using-pthread-in-wasm/15767/1 "2025-06-27T13:34:18Z")

</div>

Hello,  
I’m trying to compile VTK-9.5.0 and use the resulting build as a library for compiling my own C++ code targeting WebAssembly.

During the VTK build process, I followed the instructions from the official guide ([Building using emscripten for WebAssembly - VTK documentation](https://docs.vtk.org/en/latest/advanced/build_wasm_emscripten.html)) and enabled the option `-DVTK_WEBASSEMBLY_THREADS:BOOL=ON`.

When compiling my C++ project with this VTK build, I added the following flags in my `CMakeLists.txt`:  
`-pthread` and `-sPROXY_TO_PTHREAD=1`

However, I encountered the following error:

> –shared-memory is disallowed by vtkSMPThreadPool.cxx.o because it was not compiled with ‘atomics’ or ‘bulk-memory’ features.

Is there a specific flag or configuration I may have missed when building VTK to support threads in WASM?

Any help would be greatly appreciated!

---

<div class="post-metadata">

### Author: ![jaswantp](https://discourse.vtk.org/user_avatar/discourse.vtk.org/jaswantp/32/10046_2.png) [@jaswantp](https://discourse.vtk.org/u/jaswantp)
#### Post date: [June 27, 2025, 4:08pm UTC](https://discourse.vtk.org/t/issue-with-using-pthread-in-wasm/15767/2 "2025-06-27T16:08:00Z")

</div>

Can you search the build.ninja of VTK for instances of `-pthread`? I want confirm that the generated file has that flag for the `vtkSMPThreadPool.cxx.o`. Sometimes, setting the option after initial configure does not work.

---

<div class="post-metadata">

### Author: ![blocky](https://discourse.vtk.org/user_avatar/discourse.vtk.org/blocky/32/9531_2.png) [@blocky](https://discourse.vtk.org/u/blocky)
#### Post date: [June 30, 2025, 12:00pm UTC](https://discourse.vtk.org/t/issue-with-using-pthread-in-wasm/15767/3 "2025-06-30T12:00:19Z")

</div>

Thank you for your reply! I was previously using MinGW, but after switching to Ninja, the issue was resolved.

Now I have another question related to WebGPU. I’d like to use both multithreading and WebGPU together. Following the discussion here: [Problems when compilate VTK build for wasm - #4 by jaswantp](https://discourse.vtk.org/t/problems-when-compilate-vtk-build-for-wasm/15648/4), I integrated Demdawnwebgpu\_DIR into my build, and then encountered the same error as before, which I suspect is due to the WebGPU build not being compiled with the -pthread flag.

> –shared-memory is disallowed by webgpu.cpp.o because it was not compiled with ‘atomics’ or ‘bulk-memory’ features.

Is there a version of the webgpu that supports multithreading? Or is there any source code and guidance on how to build webgpu properly with multithreading support?

Thanks again for your help!

---

<div class="post-metadata">

### Author: ![jaswantp](https://discourse.vtk.org/user_avatar/discourse.vtk.org/jaswantp/32/10046_2.png) [@jaswantp](https://discourse.vtk.org/u/jaswantp)
#### Post date: [June 30, 2025, 12:53pm UTC](https://discourse.vtk.org/t/issue-with-using-pthread-in-wasm/15767/4 "2025-06-30T12:53:22Z")

</div>

Try a recent commit of vtk master and EMSDK \>=4.0.10 We recently updated VTK to use emdawnwebgpu via the `--use-port` flag which fixes that shared-memory disallowed by webgpu.cpp.o error.

---

<div class="post-metadata">

### Author: ![blocky](https://discourse.vtk.org/user_avatar/discourse.vtk.org/blocky/32/9531_2.png) [@blocky](https://discourse.vtk.org/u/blocky)
#### Post date: [July 1, 2025, 12:39pm UTC](https://discourse.vtk.org/t/issue-with-using-pthread-in-wasm/15767/5 "2025-07-01T12:39:23Z")

</div>

it worked, thank you!

---

<div class="post-metadata">

### Author: ![blocky](https://discourse.vtk.org/user_avatar/discourse.vtk.org/blocky/32/9531_2.png) [@blocky](https://discourse.vtk.org/u/blocky)
#### Post date: [July 1, 2025, 6:29pm UTC](https://discourse.vtk.org/t/issue-with-using-pthread-in-wasm/15767/6 "2025-07-01T18:29:25Z")

</div>

Apologies for bothering you again, but I have encountered another difficult issue:

I have attempted to compile VTK using the code below

```auto
emcmake cmake -S F:\kuaite\VTK-master\VTK-master -B . -G "Ninja" -DCMAKE_BUILD_TYPE=Release -DBUILD_SHARED_LIBS:BOOL=OFF -DVTK_ENABLE_WEBGPU:BOOL=ON 
cmake --build . 
cmake --install . --prefix .\installRelease

```

and followed the [README](https://github.com/Kitware/VTK/blob/master/Examples/Emscripten/Cxx/ConeMultiBackend/README.md) instructions to compile the `ConeMultiBackend` example. During the compilation process, I encountered several warnings and errors:

```auto
[1/1] Linking CXX executable ConeMultiBackend.js
FAILED: [code=1] ConeMultiBackend.js
C:\WINDOWS\system32\cmd.exe /C "cd . && F:\kuaite\emscripten\emsdk\upstream\emscripten\em++.bat -sWASM=1 -sMODULARIZE=1 -sASYNCIFY=1 -sALLOW_MEMORY_GROWTH=1 -sEXPORT_NAME=createConeMultiBackendModule "-sEXPORTED_RUNTIME_METHODS=['ENV']" -fwasm-exceptions -sEXCEPTION_STACK_TRACES=1 -sDEFAULT_LIBRARY_FUNCS_TO_INCLUDE=$getWasmTableEntry --js-library=F:/kuaite/VTK-master/VTK-master/Rendering/UI/vtkWebAssemblyRenderWindowInteractor.js -s FULL_ES3=1 -s MIN_WEBGL_VERSION=2 -s MAX_WEBGL_VERSION=2 --use-port=emdawnwebgpu @CMakeFiles\ConeMultiBackend.rsp -o ConeMultiBackend.js && C:\WINDOWS\system32\cmd.exe /C "cd /D F:\kuaite\VTK-master\VTK-master\Examples\Emscripten\Cxx\ConeMultiBackend\out\build && D:\cmake\bin\cmake.exe -E copy_directory F:/kuaite/VTK-master/VTK-master/Examples/Emscripten/Cxx/ConeMultiBackend/web F:/kuaite/VTK-master/VTK-master/Examples/Emscripten/Cxx/ConeMultiBackend/out/build""
em++: warning: ASYNCIFY=1 is not compatible with -fwasm-exceptions. Parts of the program that mix ASYNCIFY and exceptions will not compile. [-Wemcc]
unexpected expression type
UNREACHABLE executed at C:\b\s\w\ir\cache\builder\emscripten-releases\binaryen\src\passes\Asyncify.cpp:1146!
unexpected expression type
UNREACHABLE executed at em++: error: ''F:/kuaite/emscripten/emsdk/upstream\bin\wasm-opt' --asyncify --pass-arg=asyncify-asserts --pass-arg=asyncify-propagate-addlist '--pass-arg=asyncify-imports@env.invoke_*,env. __asyncjs__ *,*.$asyncLoad,*.fd_sync,*.emscripten_promise_await,*.emscripten_async_load_script,*.emscripten_async_wget_data,*.emscripten_idb_load,*.emscripten_idb_store,*.emscripten_idb_delete,*.emscripten_idb_exists,*.emscripten_idb_clear,*.emscripten_idb_load_blob,*.emscripten_idb_store_blob,*.emscripten_sleep,*.emscripten_wget_data,*.emscripten_scan_registers,*._load_secondary_module,*.emscripten_fiber_swap,*.SDL_Delay,*.emwgpuWaitAny' ConeMultiBackend.wasm -o ConeMultiBackend.wasm --mvp-features --enable-bulk-memory --enable-bulk-memory-opt --enable-call-indirect-overlong --enable-exception-handling --enable-multivalue --enable-mutable-globals --enable-nontrapping-float-to-int --enable-reference-types --enable-sign-ext' failed (returned 3221226505)
ninja: build stopped: subcommand failed.

```

It seems that there is a conflict between `ASYNCIFY=1` and `-fwasm-exceptions`. I attempted to remove the `-fwasm-exceptions` flag, but VTK requires exception handling during compilation, which necessitates Emscripten support. However, I recall reading somewhere that WebGPU requires ASYNCIFY support (and indeed, removing ASYNCIFY caused new errors).

Are there any compilation examples? Thanks again!

---

<div class="post-metadata">

### Author: ![jaswantp](https://discourse.vtk.org/user_avatar/discourse.vtk.org/jaswantp/32/10046_2.png) [@jaswantp](https://discourse.vtk.org/u/jaswantp)
#### Post date: [July 1, 2025, 6:56pm UTC](https://discourse.vtk.org/t/issue-with-using-pthread-in-wasm/15767/7 "2025-07-01T18:56:58Z")

</div>

That example needs updated. Replace “-sASYNCIFY=1” with

```auto
"-sJSPI=1"
"-sJSPI_EXPORTS=['__wasm_call_ctors']"

```

---

<div class="post-metadata">

### Author: ![jaswantp](https://discourse.vtk.org/user_avatar/discourse.vtk.org/jaswantp/32/10046_2.png) [@jaswantp](https://discourse.vtk.org/u/jaswantp)
#### Post date: [July 1, 2025, 8:36pm UTC](https://discourse.vtk.org/t/issue-with-using-pthread-in-wasm/15767/8 "2025-07-01T20:36:12Z")

</div>

Note that there is a bug that causes the example to not start correctly. `interactor->Start()` is now non-blocking and because of that `main()` exits and interactor goes out of scope, gets destroyed and the `vtkWebAssemblyRenderWindowInteractor::ProcessEvents` gets called on a non-existent interactor. I will make a fix shortly.

---

<div class="post-metadata">

### Author: ![jaswantp](https://discourse.vtk.org/user_avatar/discourse.vtk.org/jaswantp/32/10046_2.png) [@jaswantp](https://discourse.vtk.org/u/jaswantp)
#### Post date: [July 1, 2025, 9:54pm UTC](https://discourse.vtk.org/t/issue-with-using-pthread-in-wasm/15767/9 "2025-07-01T21:54:41Z")

</div>

Fix for interactor start and example - [https://gitlab.kitware.com/vtk/vtk/-/merge\_requests/12258](https://gitlab.kitware.com/vtk/vtk/-/merge_requests/12258)

---

<div class="post-metadata">

### Author: ![blocky](https://discourse.vtk.org/user_avatar/discourse.vtk.org/blocky/32/9531_2.png) [@blocky](https://discourse.vtk.org/u/blocky)
#### Post date: [July 2, 2025, 11:26am UTC](https://discourse.vtk.org/t/issue-with-using-pthread-in-wasm/15767/10 "2025-07-02T11:26:08Z")

</div>

Thank you so much for the fix! The example now works smoothly.  
It seems the new version introduced some changes, i will try to exploring them
