# Thread safety of vtkMPIController::SetGlobalController()

**URL:** https://discourse.vtk.org/t/thread-safety-of-vtkmpicontroller-setglobalcontroller/8650
**Category:** Support
**Created:** [June 2, 2022, 5:49pm UTC](https://discourse.vtk.org/t/thread-safety-of-vtkmpicontroller-setglobalcontroller/8650 "2022-06-02T17:49:59Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![acbauer](https://discourse.vtk.org/user_avatar/discourse.vtk.org/acbauer/32/320_2.png) [@acbauer](https://discourse.vtk.org/u/acbauer)
#### Post date: [June 2, 2022, 5:49pm UTC](https://discourse.vtk.org/t/thread-safety-of-vtkmpicontroller-setglobalcontroller/8650/1 "2022-06-02T17:49:59Z")

</div>

Hi,

I’m playing around with trying to do some pipeline processing with MPI parallelism in detached threads. As an example, I have 20 MPI processes and on 5 of them I detach a thread with their own vtkMPIController (with 5 MPI processes, I’ll call this controller **S** ) that’s a subgroup of the global vtkMPIController (with 20 MPI processes, I’ll call this controller **G** ). In the detached thread I want to set the **S** to be the “GlobalController”, i.e. do vtkMPIController::SetGlobalController( **S** );. When I do this though this actually changes the global controller in the main thread on those 5 MPI processes, i.e. if I do vtkMPIController::GetGlobalController(); I’ll get back **S** and not **G**. Should this be the expected behavior or is it a bug?

Thanks,  
Andy

ps. To be specific, what really seems to be happening is that the WorldCommunicator, e.g. what you get from `vtkMPICommunicator::GetWorldCommunicator()`, doesn’t seem to be thread safe. In essence though it’s the same issue of not being thread safe.

---

<div class="post-metadata">

### Author: ![ben.boeckel](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/b/ea5d25/32.png) [@ben.boeckel](https://discourse.vtk.org/u/ben.boeckel)
#### Post date: [June 2, 2022, 5:57pm UTC](https://discourse.vtk.org/t/thread-safety-of-vtkmpicontroller-setglobalcontroller/8650/2 "2022-06-02T17:57:08Z")

</div>

Yes, this smells a lot like there are missing locks. I went through a while ago ([!8732](https://gitlab.kitware.com/vtk/vtk/-/merge_requests/8732)) and found a lot of improper MPI controller tracking. This may be involved.

> [@acbauer](#):
>
> When I do this though this actually changes the global controller in the main thread on those 5 MPI processes

Yes, there is only one global controller. There is no per-thread MPI controller.

Also, if there are `vtkWeakPointer` instances around, these are 100% thread-unsafe (the only valid things to do are to construct and destruct them; every provided method is a potential unchecked race condition).
