C ++ compiler error "cannot be thread-local because it is of non-POD type" "

This statement:

___thread A a; 

Generates this error:

cannot be thread-local since it is a non-POD type

where A is

 class A{ public: // function declaration private: // data members }; 

I am trying to compile this on Linux, with the ogs includes and ogs mk commands. We have static threads, that is, before our application arrives, we know about the number of threads, so at present the work is done with the declaration of the array A ie

 A a[Number of threads]. 

How can i fix this?

+4
source share
2 answers

Assuming you are using gcc , threaded storage is only supported for POD types, i.e. for data only. You can try to extract the data into a separate struct and make it a local thread (in fact, this is probably a good idea anyway, because there is no point in using thread local methods).

+2
source

Unfortunately, in C ++ 03 there is nothing like dynamically initializing (and destroying) the local resources of a stream (which know nothing about threads).

In C ++ 11, the thread_local storage keyword allows dynamic initialization at the expense of the working environment (basically, equivalent to having a local static stream variable) and therefore can be used without types with constructors.

In C ++ 11, the constexpr constructor can be called for static initialization and therefore must be compatible with the __thread specifier if your compiler implements it.

+1
source

All Articles