Reputation: 51
This statement:
___thread A a;
Generates this error:
cannot be thread-local because it has non-POD type
where A is
class A{
public:
// function declaration
private:
// data members
};
I am trying this to compile on Linux, with command ogs includes & ogs mk. We have static threads i.e. before our application comes in we aware about the number of threads hence currently the work around is done with declaring the array of A i.e.
A a[Number of threads].
How can I fix this?
Upvotes: 5
Views: 2083
Reputation: 300059
Unfortunately, there is no such thing as dynamic initialization (and destruction) of thread local resources in C++03 (which know nothing about threads, anyway).
In C++11, the thread_local
storage keyword allows for dynamic initialization at the cost of runtime overhead (basically, equivalent to having a thread local static variable) and might thus be used without types with constructors.
In C++11, again, a constexpr
constructor might be call for static initialization and should thus be compatible with the __thread
specifier, provided your compiler implements it.
Upvotes: 1
Reputation: 18368
Assuming you use gcc
, thread-local storage only supported for POD
types, i.e. data-only structures.
You can attempt to extract the data into a separate struct
and make it thread local (actually, that's probably a good idea in any case because there is no much sense in making methods thread local).
Upvotes: 3