< Previous by Date Date Index Next by Date >
< Previous in Thread Thread Index Next in Thread >

Re: [reSIProcate] [reSIProcate-commit] resiprocate 8871 sgodin: -fixed potential multi-threading issues with static member variable initialization


Hi Kennard,

Static member variables are constructed the first time the function is called.  If multiple threads call a particular member function (that contains a static variable) for the first time at the exact same time (ie. thread preempts in the middle of object construction), then both threads can end up trying to construct the same static object.  I was seeing this behaviour under Visual Studio 2008, it possible that this problem does not exist on other compilers.  Moving the static variables outside of the member function causes them to be created when the application is initialized, thus avoiding this race.

Scott

On Wed, Dec 1, 2010 at 1:00 PM, Kennard White <kennard_white@xxxxxxxxxxxx> wrote:
Hi Scott,

There is some intricacy of C++ that I don't understand. Could you explain the thread safety issue below? Is there a difference in when the constructors/destructors are called?

Thanks,
Kennard

+static const Data errorContextData("Contents");
 const Data&
 Contents::errorContext() const
 {
-   static const Data context("Contents");
-   return context;
+   return errorContextData;
 }


On Sat, Nov 27, 2010 at 7:37 AM, <svn@xxxxxxxxxxxxxxx> wrote:

_______________________________________________
resiprocate-commit mailing list
resiprocate-commit@xxxxxxxxxxxxxxx
https://list.resiprocate.org/mailman/listinfo/resiprocate-commit


_______________________________________________
resiprocate-devel mailing list
resiprocate-devel@xxxxxxxxxxxxxxx
https://list.resiprocate.org/mailman/listinfo/resiprocate-devel