Suppose there is a function (member function possibly)

SomeType foo()
{
    static SomeType var = generateVar();
    return var;
}

How var will be initialised if foo will be called 'for first time' from multiple threads simultaneously?

  1. Is it guaranteed that generateVar() will be called only once in any scenario (if used of course)?
  2. Is it guaranteed that foo will return the same value when called multiple times in any scenario?
  3. Is there a difference in behaviour for primitive or non-primitive types?
Edit
Report