I declare a variable __thread int my_id
;
Info of my platform and compiler:
Configured with: ../src/configure -v --with-pkgversion='Ubuntu/Linaro 4.6.1-9ubuntu3' --with-bugurl=file:///usr/share/doc/gcc-4.6/README.Bugs --enable-languages=c,c++,fortran,objc,obj-c++,go --prefix=/usr --program-suffix=-4.6 --enable-shared --enable-linker-build-id --with-system-zlib --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --with-gxx-include-dir=/usr/include/c++/4.6 --libdir=/usr/lib --enable-nls --with-sysroot=/ --enable-clocale=gnu --enable-libstdcxx-debug --enable-libstdcxx-time=yes --enable-plugin --enable-objc-gc --enable-targets=all --disable-werror --with-arch-32=i686 --with-tune=generic --enable-checking=release --build=i686-linux-gnu --host=i686-linux-gnu --target=i686-linux-gnu
Thread model: posix
gcc version 4.6.1 (Ubuntu/Linaro 4.6.1-9ubuntu3)
I use flag -lpthread to compile.
But Compiler complains: error: storage class specified for 'my_id'
.
You've declared thread local storage for a variable. This kind of storage for a variable cannot be declared as existing on the stack. Making it part of a struct/class implicitly permits it to be on the stack.
The only effective way to accomplish this is to make the variable static
- the reason for this is that it causes the variable to be stored in the thread local storage pool instead of on the stack.
e.g. at the global scope:
__thread int my_id;
will work, as the variable is not in a structure. It can automatically be placed into the thread local storage pool.
for a structure, the following:
struct xx {
__thread int my_id;
};
will not work, as the variable can end up on the stack, so you need to use:
struct xx {
static __thread int my_id;
};
which causes it to be placed in the thread local storage pool, and thus be a valid thread variable.
Note: I'd previously described the variable as being on the heap. This is strictly speaking incorrect - the variable is taken from a per-thread block of memory that is allocated at thread creation time; I've renamed the term to the 'thread local storage' pool. This pool is of a platform specific size, and can be filled up if you have too many separate __thread
variables.
The Wikipedia entry on Thread Local Storage explains this in good detail (including some gotchas).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With