[osg-submissions] Multithreaded static initialization bugs in Inventor plugin

Alberto Luaces aluaces at udc.es
Tue Sep 17 05:18:27 PDT 2013


there is a bug report in the Ubuntu tracker that points to a bug when
loading multiple VRML files in parallel.  Christopher R. Baker has
detected this bug and crafted a patch.  In addition, libcoin has to be
also built with the "--enable-threadsafe" option.

I copy here his report, extracted from
and attach his fix.  All credit is due to him:

There are three instances of a classical method-local-static
multithreaded initialization bug in the Inventor plugin for OSG that
trigger various memory faults when reading multiple VRML files in
parallel via osgDB::readNodeFile. These bugs are of the form:

static std::map<Stuff,OtherStuff> myHandyMap;
static bool once = true;
if(once) { ...fill myHandyMap; once = false }
... use myHandyMap;

To repeat: try loading multiple VRML files from multiple threads. The
liklihood of the bug depends on many factors, but my application, which
parallel-loads some dozens of small (<100K) VRML files on startup,
triggers this problem 25% of the time or more.

The attached patch (inventor-plugin-multithread.patch) rectifies this
problem by:

1 - Inheriting MyHandyMap from std::map, then
2 - Moving the map initialization into the derived constructor, which
3 - Is intrinsically protected from multithread issues by g++ (and is
part of the C++ standard), unless you pass -fno-threadsafe-statics,
which is strongly discouraged by the man page.

-------------- next part --------------
A non-text attachment was scrubbed...
Name: ConvertFromInventor.cpp
Type: text/x-c++src
Size: 84138 bytes
Desc: not available
URL: <http://lists.openscenegraph.org/pipermail/osg-submissions-openscenegraph.org/attachments/20130917/5638442c/attachment-0001.cpp>
-------------- next part --------------



More information about the osg-submissions mailing list