# Deprecate vtkMutexLock and vtkAtomic ?

**URL:** <https://discourse.vtk.org/t/deprecate-vtkmutexlock-and-vtkatomic/772>\
**Category:** Development\
**Created:** [April 17, 2019, 8:50am UTC](https://discourse.vtk.org/t/deprecate-vtkmutexlock-and-vtkatomic/772 "2019-04-17T08:50:59Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![mwestphal](https://discourse.vtk.org/user_avatar/discourse.vtk.org/mwestphal/32/19_2.png) [@mwestphal](https://discourse.vtk.org/u/mwestphal)\
**Post date:** [April 17, 2019, 8:50am UTC](https://discourse.vtk.org/t/deprecate-vtkmutexlock-and-vtkatomic/772/1 "2019-04-17T08:50:59Z")

</div>

vtkMutexLock is useless since C++11 support and std::mutex.

Should we deprecate it for further removal ?

---

<div class="post-metadata">

**Author:** ![mwestphal](https://discourse.vtk.org/user_avatar/discourse.vtk.org/mwestphal/32/19_2.png) [@mwestphal](https://discourse.vtk.org/u/mwestphal)\
**Post date:** [April 17, 2019, 8:54am UTC](https://discourse.vtk.org/t/deprecate-vtkmutexlock-and-vtkatomic/772/2 "2019-04-17T08:54:06Z")

</div>

The same could be said about vtkAtomic and std::atomic. The work to remove it may be bigger though.

---

<div class="post-metadata">

**Author:** ![Dave\_DeMarle](https://discourse.vtk.org/user_avatar/discourse.vtk.org/dave_demarle/32/6_2.png) [@Dave\_DeMarle](https://discourse.vtk.org/u/Dave_DeMarle)\
**Post date:** [April 17, 2019, 1:54pm UTC](https://discourse.vtk.org/t/deprecate-vtkmutexlock-and-vtkatomic/772/3 "2019-04-17T13:54:44Z")

</div>

If we can do so without much upgrade pain for applications that use VTK then I say heck yeah.

---

<div class="post-metadata">

**Author:** ![JPouderoux](https://discourse.vtk.org/user_avatar/discourse.vtk.org/jpouderoux/32/316_2.png) [@JPouderoux](https://discourse.vtk.org/u/JPouderoux)\
**Post date:** [April 17, 2019, 3:02pm UTC](https://discourse.vtk.org/t/deprecate-vtkmutexlock-and-vtkatomic/772/4 "2019-04-17T15:02:25Z")

</div>

Always remember that standard is one thing, implementation is another.  
See [https://en.cppreference.com/w/cpp/compiler\_support#cpp11](https://en.cppreference.com/w/cpp/compiler_support#cpp11)

However, regarding mutex and atomic, I think they are now quite well supported.  
vtkAtomic has different implementations depending on the selected SMP backend. Could there a benefit to use TBB atomics instead of C++11 ones?

---

<div class="post-metadata">

**Author:** ![mwestphal](https://discourse.vtk.org/user_avatar/discourse.vtk.org/mwestphal/32/19_2.png) [@mwestphal](https://discourse.vtk.org/u/mwestphal)\
**Post date:** [April 17, 2019, 4:59pm UTC](https://discourse.vtk.org/t/deprecate-vtkmutexlock-and-vtkatomic/772/5 "2019-04-17T16:59:09Z")

</div>

@will.schroeder : do you have an advice here ?

---

<div class="post-metadata">

**Author:** ![mwestphal](https://discourse.vtk.org/user_avatar/discourse.vtk.org/mwestphal/32/19_2.png) [@mwestphal](https://discourse.vtk.org/u/mwestphal)\
**Post date:** [April 18, 2019, 7:42am UTC](https://discourse.vtk.org/t/deprecate-vtkmutexlock-and-vtkatomic/772/6 "2019-04-18T07:42:04Z")

</div>

I may give it a go during the Hackaton.

---

<div class="post-metadata">

**Author:** ![RobertMaynard](https://discourse.vtk.org/user_avatar/discourse.vtk.org/robertmaynard/32/24_2.png) [@RobertMaynard](https://discourse.vtk.org/u/RobertMaynard)\
**Post date:** [April 18, 2019, 12:39pm UTC](https://discourse.vtk.org/t/deprecate-vtkmutexlock-and-vtkatomic/772/7 "2019-04-18T12:39:58Z")

</div>

I expect that `std::atomic` will be an easy replacement for `vtkAtomic` when `T` is a primitive type. The open question is how well specified `std::atomic<void*>` is, as `vtkAtomic` also supports that.

---

<div class="post-metadata">

**Author:** ![mwestphal](https://discourse.vtk.org/user_avatar/discourse.vtk.org/mwestphal/32/19_2.png) [@mwestphal](https://discourse.vtk.org/u/mwestphal)\
**Post date:** [April 18, 2019, 1:21pm UTC](https://discourse.vtk.org/t/deprecate-vtkmutexlock-and-vtkatomic/772/8 "2019-04-18T13:21:28Z")

</div>

one information : std::mutex does not support forward declaration

---

<div class="post-metadata">

**Author:** ![RobertMaynard](https://discourse.vtk.org/user_avatar/discourse.vtk.org/robertmaynard/32/24_2.png) [@RobertMaynard](https://discourse.vtk.org/u/RobertMaynard)\
**Post date:** [April 18, 2019, 1:27pm UTC](https://discourse.vtk.org/t/deprecate-vtkmutexlock-and-vtkatomic/772/9 "2019-04-18T13:27:12Z")

</div>

It is undefined behavior to forward declare anything from that exists in the `std::` namespace.

---

<div class="post-metadata">

**Author:** ![mwestphal](https://discourse.vtk.org/user_avatar/discourse.vtk.org/mwestphal/32/19_2.png) [@mwestphal](https://discourse.vtk.org/u/mwestphal)\
**Post date:** [April 18, 2019, 1:28pm UTC](https://discourse.vtk.org/t/deprecate-vtkmutexlock-and-vtkatomic/772/10 "2019-04-18T13:28:19Z")

</div>

indeed.

---

<div class="post-metadata">

**Author:** ![mwestphal](https://discourse.vtk.org/user_avatar/discourse.vtk.org/mwestphal/32/19_2.png) [@mwestphal](https://discourse.vtk.org/u/mwestphal)\
**Post date:** [April 18, 2019, 2:45pm UTC](https://discourse.vtk.org/t/deprecate-vtkmutexlock-and-vtkatomic/772/11 "2019-04-18T14:45:13Z")

</div>

here is a WIP MR : [https://gitlab.kitware.com/vtk/vtk/merge\_requests/5424](https://gitlab.kitware.com/vtk/vtk/merge_requests/5424)  
It replace vtkMutexLock by std::mutex. Seems to work fine so far.

The porting is quite simple : `#include <mutex>`, replace `vtkMutexLock` by `std::mutex` , replace `Lock()` by `lock()` and `Unlock()` by `unlock()` and you’re done.

However there are other classes that would need to be considered as well :  
vtkCriticalSection  
vtkSimpleCriticalSection  
vtkConditionVariable

---

<div class="post-metadata">

**Author:** ![kayarre](https://discourse.vtk.org/user_avatar/discourse.vtk.org/kayarre/32/808_2.png) [@kayarre](https://discourse.vtk.org/u/kayarre)\
**Post date:** [November 11, 2021, 8:25pm UTC](https://discourse.vtk.org/t/deprecate-vtkmutexlock-and-vtkatomic/772/12 "2021-11-11T20:25:20Z")

</div>

> [@mwestphal](#):
>
> vtkCriticalSection  
> vtkSimpleCriticalSection  
> vtkConditionVariable

What needs to happen for the other classes can they be replaced similar to what you proposed?

will this work?

> [@mwestphal](#):
>
> The porting is quite simple : `#include <mutex>`, replace `vtkMutexLock` by `std::mutex` , replace `Lock()` by `lock()` and `Unlock()` by `unlock()` and you’re done.

I found this as well which I think gets pretty close to making it make sense. [https://gitlab.kitware.com/vtk/vtk/-/merge\_requests/7224](https://gitlab.kitware.com/vtk/vtk/-/merge_requests/7224)

and this: [documentation from gitlab](https://gitlab.kitware.com/vtk/vtk/-/blob/c43435ec445180fa857b611bfda970ef53327adc/Documentation/release/dev/threading-cleanup.md)

Making more sense now.

---

<div class="post-metadata">

**Author:** ![mwestphal](https://discourse.vtk.org/user_avatar/discourse.vtk.org/mwestphal/32/19_2.png) [@mwestphal](https://discourse.vtk.org/u/mwestphal)\
**Post date:** [November 11, 2021, 10:47pm UTC](https://discourse.vtk.org/t/deprecate-vtkmutexlock-and-vtkatomic/772/13 "2021-11-11T22:47:35Z")

</div>

Yes, this is the same idea, more work to be done though.

---

<div class="post-metadata">

**Author:** ![kayarre](https://discourse.vtk.org/user_avatar/discourse.vtk.org/kayarre/32/808_2.png) [@kayarre](https://discourse.vtk.org/u/kayarre)\
**Post date:** [November 12, 2021, 1:30am UTC](https://discourse.vtk.org/t/deprecate-vtkmutexlock-and-vtkatomic/772/14 "2021-11-12T01:30:05Z")

</div>

Thank you @mwestphal. I think I was able to resolve my issue following the current updates.

> [@mwestphal](#):
>
> more work to be done though.

What is missing?

---

<div class="post-metadata">

**Author:** ![mwestphal](https://discourse.vtk.org/user_avatar/discourse.vtk.org/mwestphal/32/19_2.png) [@mwestphal](https://discourse.vtk.org/u/mwestphal)\
**Post date:** [November 12, 2021, 8:43am UTC](https://discourse.vtk.org/t/deprecate-vtkmutexlock-and-vtkatomic/772/15 "2021-11-12T08:43:58Z")

</div>

> [@mwestphal](#):
>
> vtkCriticalSection  
> vtkSimpleCriticalSection  
> vtkConditionVariable

Neverming, looking at @seanm MR, looks like all have been adressed !
