1

I want a variable template to be threadprivate as well. Could this be done ? Something like the following:

template<typename T>
struct Tpl { };

template<typename X>
extern Tpl<X> var;
#pragma omp threadprivate(var) // <<< No compiler supports this.
template<typename X>
Tpl<X> var;
Zulan
  • 21,896
  • 6
  • 49
  • 109
user416983
  • 974
  • 3
  • 18
  • 28
  • 1
    If there is no better solution, you may try to give the variable the C++11 `thread_local` specifier. AFAIK it is not guaranteed to be compatible with OpenMP, but often is. – michalsrb Dec 05 '16 at 15:29
  • I'm currently using a tricky hack: wrap all access to `var` with a class template `template Access`, then specialize all possible `X` for class `Access` in my program, and in the specialized methods, access different non-template `threadprivate` variables like `var_int`, `var_double`, etc. It works, but boring to implement. – user416983 Dec 05 '16 at 16:12
  • Can you elaborate more on why you need a templated variable here? Surely your worker function will only access a small predetermined number of its incarnations (as your "tricky hack" seems to confirm). – The Vee Dec 05 '16 at 17:44
  • Please note that this concept is called *[variable template](http://en.cppreference.com/w/cpp/language/variable_template)*, there is no such thing as a *template variable* in C++. – Zulan Dec 05 '16 at 18:30
  • @Zulan Thanks for the info & edit. – user416983 Dec 06 '16 at 06:44
  • @TheVee The actual code is much more complicated. In the actual code, the variable template is a special memory allocator that isn't thread-safe. – user416983 Dec 06 '16 at 06:51

0 Answers0