Nick
Nick

Reputation: 2050

C++, Is static initialisation performed by multiple threads?

In C++, do I have to worry about multiple threads operating on an object during static initialisation or is it guaranteed to be performed by a single thread?

Upvotes: 1

Views: 152

Answers (2)

dlf
dlf

Reputation: 9393

One little caveat: do any of your global-scope static objects spawn threads from their constructors? If so, those threads will be running during static initialization time.

Upvotes: 0

Luchian Grigore
Luchian Grigore

Reputation: 258638

For global-scoped static variables, yes.

For local statics (declared inside functions), this guarantee only holds in C++11 and after.

Upvotes: 2

Related Questions