Advertisement Jump to content
Sign in to follow this  
L. Spiro

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

This topic is 1871 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post


Link to post
Share on other sites
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.

Edited by Mona2000

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites


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.

Share this post


Link to post
Share on other sites

 


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.

Share this post


Link to post
Share on other sites

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

 

 

L. Spiro

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!