3

Imagin this:

void *ImCalledByThreads (/*...*/)
{
    //some stuff
    static typePlaceholder AmIThreadSafe = QuestionTag();
    //other stuff
}

Is this initialisation threadsafe?

Even if QuestionTag() is threadsafe, What happens if the first thread runs the function and another invokes this line. Would there be a detection that the function will only be executed once? or is this simply just a bad idea for multithreading?

dhein
  • 6,431
  • 4
  • 42
  • 74
  • 2
    That's not legal C code. But if you're talking about C++ instead of C, it's almost certainly not thread-safe. I'm not sure what C++11 says, but C++03 and earlier don't mention threads at all. And from the generated code I've looked at from various compilers, they don't add any synchronization primitives around the variable initialization. – Adam Rosenfield Dec 18 '13 at 22:49
  • 1
    @AdamRosenfield It's thread safe in gcc(c++) for a while, C++11 mandates it to be – nos Dec 18 '13 at 22:53
  • Stupid MSVC.... I'm talking about pure C. But I just tested this on windows and no errors... But I forgot that this is not reliable to C standard what MSVC expects as C code. Thanks anyway. – dhein Dec 18 '13 at 22:56
  • 2
    It is not a duplicate, since i didn't ask about C++ – dhein Dec 18 '13 at 22:58
  • @Zaibis are you certain MSVC compiles your code as C, not C++ ? At any rate, C or C++, it's not threadsafe in msvc until VS 2013. – nos Dec 18 '13 at 22:59
  • @nos Yeah I'm pretty sure that its set to C code, as I try to avoid c++ when ever its possible. But I'm using MSVC 2010 and jsut used it for compilation check. Mainly I'm using gcc on FreeBSD 9.2 – dhein Dec 18 '13 at 23:00
  • @nos: Good point, my memory is a little fuzzy. My current versions of G++ and Clang both do insert the appropriate locks around static initializers even when operating in C++03 mode. – Adam Rosenfield Dec 18 '13 at 23:01
  • @Zaibis: Sorry, got the language confused. In C, static initializers can only be constant expressions, so there's nothing to be thread-safe. – Kerrek SB Dec 18 '13 at 23:44

1 Answers1

3

It's not legal C code, since C requires initializers of static variables to be compile-time constants. Hence the initialization can happen at program load time, before any threads have a chance to be started, so there can be no race conditions there.

From C99 §6.7.8/4:

All the expressions in an initializer for an object that has static storage duration shall be constant expressions or string literals.

Visual Studio may allow non-constants as a non-standard extension, but for that, all bets are off. Check its documentation and/or its generated assembly code to see if it's thread-safe or not.

For C++, where non-constants initializers are allowed, see the question Is initialization of local static function-object thread-safe?. Short answer: Yes, in C++11, no in C++03 and earlier (which don't mention threads in the standard), though compilers may still make it thread-safe if they choose.

Community
  • 1
  • 1
Adam Rosenfield
  • 390,455
  • 97
  • 512
  • 589