in this code:
int foo() {
static int x;
}
is the x global to all threads or local in each thread? Or does that depends on a compiler flag and/or the compiler, so I cannot really know what it is from the code?
Several questions (all of them independent from compiler and compiler flags and OS):
- How can I create a static variable which is global to all threads?
- How can I create a static variable which is local to each thread?
- How can I create a global variable which is global to all threads?
- How can I create a global variable which is local to each thread?
I guess that this is not in C++ itself. (Is it in C++0x?) Some Boost lib which can do this?
x is global to all threads. Always, independent of compiler and/or its flags. Independent of whether this is in C++11 or C++03. So if you declare a regular global or static local variable, it will be shared between all threads.
In C++11 we will have the
thread_localkeyword. Until then, you can use thread_specific_ptr from Boost.Thread.