vtkMath::Random assertion and abort - lack of thread safety
When running vtkMath::Random(-1, 1)
simultaneous in several threads, then an assertion is raised and the program aborts.
It's likely due to lack of thread safety in vtkMath
and/or in vtkMinimalStandardRandomSequence
. vtkMath::Internal
is static
and that means that vtkMinimalStandardRandomSequence::Seed
could be modified concurrently.
To reproduce with assertion activated in VTK and TBB SMP backend:
#include <vtkMath.h>
#include <vtkSMPTools.h>
#include <array>
int main(int, char*[])
{
std::cout << "backend: " << vtkSMPTools::GetBackend() << std::endl;
std::cout << "estimated number of threads: " << vtkSMPTools::GetEstimatedNumberOfThreads() << std::endl;
const size_t size = 10000;
std::array<double, size> results;
vtkSMPTools::For(0,size, [&](vtkIdType start, vtkIdType stop){
for(vtkIdType i = start; i < stop; ++i)
{
results[i] = vtkMath::Random(-1,1);
}
});
return EXIT_SUCCESS;
}
It outputs:
backend: TBB
estimated number of threads: 12
vtk_test: /home/francois/dev/vtk/VTK/Common/Core/vtkMinimalStandardRandomSequence.cxx:79: virtual double vtkMinimalStandardRandomSequence::GetValue(): Assertion `"post: unit_range" && result >= 0.0 && result <= 1.0' failed.
For the context, I discovered it from a random crash in ParaView when applying the "Resample to Image" filter.
Edited by Francois Mazen