VTK WebAssembly, vtkImageCanvasSource2D, and pthread_attr_setscope issue

I am trying to use vtkImageCanvasSource2D with WebAssembly and I am getting a runtime error “missing function: pthread_attr_setscope”. I think this is called when vtkImageCanvasSource2D updates. When I compile the project with Emscripten I always get the warning “warning: undefined symbol: pthread_attr_setscope”, but it wasn’t causing any issues until I started using the class vtkImageCanvasSource2D. I found this link: https://github.com/InsightSoftwareConsortium/itk-js/issues/247 and I added vtkThreadedImageAlgorithm::SetGlobalDefaultEnableSMP(true); at the beginning of my program and the runtime error disappears, but my image doesn’t get rendered. I can load a .vti file with vtkXMLImageDataReader and everything renders fine. I need to create my own vtkImageData at runtime with the vtkImageCanvasSource2D functionality. I can run the same program with VTK C++ on my Windows machine and the image renders fine. Seems to be a problem with that class, WebAssembly, and pthreads. Any help is appreciated.

I think WebAssembly does not support pthread hence the issue. But there might be some option now (compilation flag or else) to handle that (but don’t know). Searching online might allow you to find a possible path forward.

Thanks Sebastien. I was thinking of recompiling VTK for wasm without pthread support, but I am unable to find a cmake definition to disable pthreads it in VTK. Does anybody know how to disable pthreads when compiling VTK? Here is the configuration I used to compile VTK for wasm:

cmake
-G Ninja
-DCMAKE_TOOLCHAIN_FILE=${CMAKE_TOOLCHAIN_FILE}
-DBUILD_SHARED_LIBS:BOOL=OFF
-DCMAKE_BUILD_TYPE:STRING=Release
-DVTK_ENABLE_LOGGING:BOOL=OFF
-DVTK_ENABLE_WRAPPING:BOOL=OFF
-DVTK_LEGACY_REMOVE:BOOL=ON
-DVTK_OPENGL_USE_GLES:BOOL=ON
-DVTK_USE_SDL2:BOOL=ON
-DVTK_NO_PLATFORM_SOCKETS:BOOL=ON
-DVTK_MODULE_ENABLE_VTK_hdf5:STRING=NO
/work/src

I found the problem. I am reading a binary file that contains the data I am creating the vtkImageData with. When running on Windows and I need to create a short from two bytes this works fine:

((char)(a)) | (((short)((char)(b)))<<8))

It does not work the same when compiled with Emscripten. I changed to:

(((short)a) << 8) | (0x00ff & b)

and it works on both platforms.

I still have to add:

vtkThreadedImageAlgorithm::SetGlobalDefaultEnableSMP(true);

at the beginning of my program to get rid of the “missing function: pthread_attr_setscope” runtime error.