[gdal-dev] How to correctly initialize GDAL library in a multithreaded environment?

Even Rouault even.rouault at spatialys.com
Mon Jan 14 03:16:25 PST 2019


Hi,

> 
> I have a multithreaded application that uses GDAL. Generally it works fine,
> but sometimes I have a hang when trying to create transformations from
> different threads.
> 
> Seems like race condition occures when initializing static CPLMutex
> *hConfigMutex located in gdal\port\cpl_conv.cpp file;

On which operating system and with which GDAL version ?
With recent enough GDAL versions, and on Linux/Unix, I believe that mutex 
creation should now be thread-safe. But there might still be issues on 
Windows. We should be able to fix that more easily with C++11 mutex now.

Please file a ticket about that on
https://github.com/OSGeo/gdal/issues with the above requested details.

> 
> I was able to fix the issue by calling a function that initializes the mutex
> before calls from different threads(just calling the following code:
> CPLGetConfigOption("GDAL_DATA", nullptr);) but it doesn't feel right.

Calling a function like GDALRegisterAll() that involves mutex creation before 
creating any thread has been the historical recommandation

Even

-- 
Spatialys - Geospatial professional services
http://www.spatialys.com


More information about the gdal-dev mailing list