DicomImage() constructor is NOT thread safe

All other questions regarding DCMTK

Moderator: Moderator Team

Post Reply
Message
Author
artemmikheev
Posts: 24
Joined: Mon, 2018-11-12, 20:01

DicomImage() constructor is NOT thread safe

#1 Post by artemmikheev » Wed, 2020-08-19, 14:49

Hi there,
I am trying to get access to the pixel data of individual frames in Multi-frame DICOM file.
I am using the following code:

DicomImage** ImgArr;

......
......

for ( int fi=0; fi<NumFrames; fi++ ) {
ImgArr[fi] = new DicomImage((DcmObject*)dataset,Tsyntax,0,unsigned long(fi),unsigned long(1));
}

All is working as expected in serial mode as above.

My file is very large (3680 frames) so it takes long time. So if I try to parallelize it, like this:

parallel_for(size_t(0), size_t(NumFrames), size_t(1), [=](size_t fi ) {
ImgArr[fi] = new DicomImage((DcmObject*)dataset,Tsyntax,0,unsigned long(fi),unsigned long(1));
});

Is this a known "feature"? Is there a workaround?

Thank you in advance.

Marco Eichelberg
OFFIS DICOM Team
OFFIS DICOM Team
Posts: 1280
Joined: Tue, 2004-11-02, 17:22
Location: Oldenburg, Germany
Contact:

Re: DicomImage() constructor is NOT thread safe

#2 Post by Marco Eichelberg » Thu, 2020-08-27, 12:09

The problem is that you pass the same DcmDataset object to all constructors, which is not supported. DcmDataset loads larger attribute values from the file in a lazy "on demand" pattern when they are accessed first. Probably all of your constructors trigger this behaviour in parallel. It might help to call dataset->loadAllDataIntoMemory() before creating the DicomImage instances. This causes the complete dataset (including the pixel data) to be read into main memory. Then you should probably create the first DicomImage instance serially, which will cause the byte order in the loaded data to be adjusted to the CPU byte order (this also only happens at first access). After that, perhaps creating further DicomImage instances for the remaining frames might work in parallel, but I can't guarantee that.

Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest