How to use VTK in multi-threaded context?

How to use VTK in multi-threaded context?

In some examples where VTK can be used along with OpenGL and GLEW (using vtkOpenGLRenderWindow or vtkVRRenderWindow for instance), my understanding is that, depending on cases, you’ll have to handle by yourself (as VTK is used in some kind of external window and no more in it’s native viewer) up to 3 major problematics:

  1. rendering
  2. propagate events to interactor and interactor style (dispatch) to move into the scene
  3. modify the scene (actors or filters for instance)

Say 1 and 2 are handled by the main thread, and, 3 is handled by a concurrent thread (spawned by the main thread).

I understand VTK is not thread-safe: https://vtk.org/pipermail/vtkusers/2015-September/092326.html. Using mutex to “lock” the scene between 1 and 3 make sense: you should not modify/render what you render/modify.

Questions:

  1. Is mutex needed also for 2 or is this part guaranteed to be thread-safe? (such that using mutex for 2 would be overkill and degrades performance)
  2. What is the best practice recommended? Is mutex a good choice or are there better ways?

Hello,

I use mutexes all the time to prevent race condition when calling libraries known to not behave safely in multithreaded calls, including the older parts of VTK.

best,

PC