C++11, Function-Local Static Variables, and Multithreading

Started by
6 comments, last by MJP 10 years, 4 months ago

A function is called on multiple threads and has a static local. It is extremely unlikely, but possible that it be called on 2 threads for the first time and the static variable initialized twice.

In old C++ this would be undefined behavior, I believe.

Does anyone know what the C++11 standard says about this? Are there memory barriers or anything to protect against this?

L. Spiro

I restore Nintendo 64 video-game OST’s into HD! https://www.youtube.com/channel/UCCtX_wedtZ5BoyQBXEhnVZw/playlists?view=1&sort=lad&flow=grid

Advertisement

Initialization was made thread safe in C++11, but MSVC doesn't honor it yet (it's planned for the next CTP IIRC). In GCC it's been thread safe for a long time.

The C++11 standard requires thread-safety; see the accepted answer here:

http://stackoverflow.com/questions/8102125/is-local-static-variable-initialization-thread-safe-in-c11

However, it depends on your compiler. VS 2013 still doesn't follow the rules, if I understand the following sentence correctly:

"Assigning a value to a static local variable in a multithreaded application is not thread safe and we do not recommend it as a programming practice."

Found here:

http://msdn.microsoft.com/en-us/library/s1sb61xd.aspx

Luckily, only our IDE is Visual Studio 2012, but the actual compiler is Sony’s for PlayStation Vita, which is a heavily modified GCC compiler.

Thank you.

L. Spiro

I restore Nintendo 64 video-game OST’s into HD! https://www.youtube.com/channel/UCCtX_wedtZ5BoyQBXEhnVZw/playlists?view=1&sort=lad&flow=grid


Luckily, only our IDE is Visual Studio 2012, but the actual compiler is Sony’s for PlayStation Vita, which is a heavily modified GCC compiler.

I would still check the disassembly to be sure, don't know which GCC version the Vita compiler is based on.


Luckily, only our IDE is Visual Studio 2012, but the actual compiler is Sony’s for PlayStation Vita, which is a heavily modified GCC compiler.

I would still check the disassembly to be sure, don't know which GCC version the Vita compiler is based on.

Not being familiar with the Vita, I'd guess the ancient GCC 4.2 (the last GPLv2 release) though at least one website indicates it may be GCC 4.6. The explicit support for thread-safe static variables in the officially supported ABIs was added in GCC 4.3. Even if Sony is using GCC 4.6 if they added a custom ARM ABI it may not support the feature. A quick test with gcc.godbolt.org indicates that Ubuntu's version of GCC 4.6's ARM compiler does support it (see use of guards and __cxa_guard_acquire in the ARM assembly output at that link). I don't have a way to test Vita's toolchain on hand.

Sean Middleditch – Game Systems Engineer – Join my team!

My check of the actual assembly shows __cxa_guard_acquire()/__cxa_guard_release(). So that’s that. Thank you.

L. Spiro

I restore Nintendo 64 video-game OST’s into HD! https://www.youtube.com/channel/UCCtX_wedtZ5BoyQBXEhnVZw/playlists?view=1&sort=lad&flow=grid

Initialization was made thread safe in C++11, but MSVC doesn't honor it yet (it's planned for the next CTP IIRC). In GCC it's been thread safe for a long time.

The CTP is actually already available.

This topic is closed to new replies.

Advertisement