[Development] Local static variable initialization in multithreaded code

Vincas Dargis vindrg at gmail.com
Sun Feb 28 15:08:44 CET 2016


Hello,

If Qt 5.7 are going to support only "proper" C++11 compilers, can new Qt code assume that function local static variable 
initialization is thread-safe?

Qt 5.7 Wiki page [1] makes me think that only Visual Studio 2013 and above are supported, is that right? If so, it 
appears that "magic statics" are implemented in 2013 RC [2] .

So, is it appropriate to write code like this:

// might be called in multiple threads
int Foo::bar() {
     static QAtomicInteger<unsigned int> count;
     return count.fetchAndAddRelaxed(1);
}

I am thinking maybe I could refactor small helper function qMakePreparedStmtId() [3] to use QAtomicInteger<unsigned int> 
instead of unsigned int and mutex.

Thanks.

[1] https://wiki.qt.io/Qt-5.7.0-tools-and-versions
[2] http://herbsutter.com/2013/09/09/visual-studio-2013-rc-is-now-available/
[3] http://code.qt.io/cgit/qt/qtbase.git/tree/src/sql/drivers/psql/qsql_psql.cpp?h=v5.5.1#n594




More information about the Development mailing list