Using the OpenMP threadprivate directive on static instances of C++ STL types

前端 未结 3 1658
星月不相逢
星月不相逢 2021-01-18 09:22

Consider the following snippet:

#include 

class A {
    static std::map theMap;
#pragma omp threadprivate(theMap)
};

std::map<         


        
3条回答
  •  旧时难觅i
    2021-01-18 09:53

    Anything threadprivate will be replicated for each thread. I have done this by making a static object (class does not need to be static, just the instantiated object must be static). Maybe this is what you want?

    Now consider if you want some members of the class to be shared between threads. Making only some members of the class static implies that if each thread instantiated that object, then we should replicate only the static part (because it's threadprivate) but not the entire object (shared memory is not replicated). This would require one object to have everything and all the other objects to be of smaller size (not re-storing shared memory) but still have a reference to the shared memory, which quite frankly doesn't make sense.

    As a suggestion, make yourself two classes, one with strictly (thread)private data and one for shared data.

提交回复
热议问题