# VTK WASM with Threading enabled: Rendering not possible in main thread?

**URL:** https://discourse.vtk.org/t/vtk-wasm-with-threading-enabled-rendering-not-possible-in-main-thread/15191
**Category:** Web
**Created:** [January 21, 2025, 9:11am UTC](https://discourse.vtk.org/t/vtk-wasm-with-threading-enabled-rendering-not-possible-in-main-thread/15191 "2025-01-21T09:11:21Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![Sophonet](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/s/ecccb3/32.png) [@Sophonet](https://discourse.vtk.org/u/Sophonet)
#### Post date: [January 21, 2025, 9:11am UTC](https://discourse.vtk.org/t/vtk-wasm-with-threading-enabled-rendering-not-possible-in-main-thread/15191/1 "2025-01-21T09:11:21Z")

</div>

Hi all,

in my VTK WASM app, I recently wanted to explore whether rendering can be accelerated by turning on threading. My plan was to keep rendering in the main thread, just to make it faster for multi-threaded rendering algorithms (like vtkCutter). However, when not setting -sPROXY\_TO\_PTHREAD=1, the console shows an error “Tried to spawn a new thread, but the thread pool is exhausted.” when invoking the rendering pipeline.

When setting PROXY\_TO\_PTHREAD and OFFSCREENCANVAS\_SUPPORT to 1, the problem does not occur, even though I would think the number of available threads is the same (actually in both cases, vtkSMPTools::GetEstimatedDefaultNumberOfThreads() called at the beginning of main() returns 4. The problem with moving rendering to an own thread, besides the need to proxy all related embind calls to the rendering thread, is the following: I need to change the canvas size from time to time with JavaScript, but this does not seem to be possible with an offscreen canvas (the problem has been posted here [javascript - Is it possible to resize a canvas after transferControlToOffscreen() in wasm? - Stack Overflow](https://stackoverflow.com/questions/77401349/is-it-possible-to-resize-a-canvas-after-transfercontroltooffscreen-in-wasm)).

Are there any hints what I can do to keep rendering in the main thread while still being able to use threading for acceleration? And if not, what I can do to change the offscreen canvas size later with JavaScript?

Thanks,

Sophonet

---

<div class="post-metadata">

### Author: ![Sophonet](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/s/ecccb3/32.png) [@Sophonet](https://discourse.vtk.org/u/Sophonet)
#### Post date: [February 3, 2025, 5:00pm UTC](https://discourse.vtk.org/t/vtk-wasm-with-threading-enabled-rendering-not-possible-in-main-thread/15191/2 "2025-02-03T17:00:41Z")

</div>

Found out some information myself: (1) multi-threading for algorithms in VTK causes the calling thread to wait for their results with pthread\_join(). This is discouraged in emscripten leads to warnings unless explicitly allowed, so I rather chose to use PROXY\_TO\_PTHREAD. (2) A canvas element that is changed to offscreen indeed loses with and height attributes (they are available at the offscreen canvas then) but not their corrresponding css elements.
