diff --git a/Modules/IO/ImageIO/include/otbImageFileWriter.h b/Modules/IO/ImageIO/include/otbImageFileWriter.h index b7bc6499b089a47cf7c75822523e56fb9cf819e0..93be343529698d6e01ab6f349bbaaa34ad3581ea 100644 --- a/Modules/IO/ImageIO/include/otbImageFileWriter.h +++ b/Modules/IO/ImageIO/include/otbImageFileWriter.h @@ -25,6 +25,7 @@ #include "itkProcessObject.h" #include "otbStreamingManager.h" #include "otbExtendedFilenameToWriterOptions.h" +#include "itkFastMutexLock.h" namespace otb { @@ -199,6 +200,11 @@ public: itkGetObjectMacro(ImageIO, otb::ImageIOBase); itkGetConstObjectMacro(ImageIO, otb::ImageIOBase); + // the interface of the superclass getter function is not thread safe + bool GetAbortGenerateDataMutex() const; + + void SetAbortGenerateData(bool val) override; + protected: ImageFileWriter(); ~ImageFileWriter() ITK_OVERRIDE; @@ -270,6 +276,9 @@ private: * This variable can be the number of components in m_ImageIO or the * number of components in the m_BandList (if used) */ unsigned int m_IOComponents; + + /** Lock to ensure thread-safety (added for the AbortGenerateData flag) */ + itk::SimpleFastMutexLock m_Lock; }; } // end namespace otb diff --git a/Modules/IO/ImageIO/include/otbImageFileWriter.txx b/Modules/IO/ImageIO/include/otbImageFileWriter.txx index b1472291f1ae216f302757ad00b69bfd161b3630..454fdea478aa9d4a9533d70b6da862a43256d296 100644 --- a/Modules/IO/ImageIO/include/otbImageFileWriter.txx +++ b/Modules/IO/ImageIO/include/otbImageFileWriter.txx @@ -616,7 +616,7 @@ ImageFileWriter<TInputImage> } for (m_CurrentDivision = 0; - m_CurrentDivision < m_NumberOfDivisions && !this->GetAbortGenerateData(); + m_CurrentDivision < m_NumberOfDivisions && !this->GetAbortGenerateDataMutex(); m_CurrentDivision++, m_DivisionProgress = 0, this->UpdateFilterProgress()) { streamRegion = m_StreamingManager->GetSplit(m_CurrentDivision); @@ -645,7 +645,7 @@ ImageFileWriter<TInputImage> * If we ended due to aborting, push the progress up to 1.0 (since * it probably didn't end there) */ - if (!this->GetAbortGenerateData()) + if (!this->GetAbortGenerateDataMutex()) { this->UpdateProgress(1.0); } @@ -843,6 +843,27 @@ ImageFileWriter<TInputImage> return this->m_FilenameHelper->GetSimpleFileName(); } +template <class TInputImage> +bool +ImageFileWriter<TInputImage> +::GetAbortGenerateDataMutex() const +{ + m_Lock.Lock(); + bool ret = Superclass::GetAbortGenerateData(); + m_Lock.Unlock(); + return ret; +} + +template <class TInputImage> +void +ImageFileWriter<TInputImage> +::SetAbortGenerateData(bool val) +{ + m_Lock.Lock(); + Superclass::SetAbortGenerateData(val); + m_Lock.Unlock(); +} + } // end namespace otb #endif