1
这里决定变量的类型是一个图书馆的收缩我的工作:在编译的时候C++
class pool
{
private:
std::vector<int> m_buffer;
public:
void insert(int a) { m_buffer.push_back(a); }
int size() { return (int)(m_buffer.size()); }
virtual ~pool() {}
};
class customMem
{
public:
static thread_local pool poolmanager;
static boost::thread_specific_ptr< pool> poolmanager_boost;
};
thread_local pool customMem::poolmanager;
boost::thread_specific_ptr<pool> customMem::poolmanager_boost; //very slow
class MVeryLongData : public customMem
{
public:
MVeryLongData(bool localthread, int a)
{
if (localthread)
{
customMem::poolmanager.insert(a);
}
else
{
if (!customMem::poolmanager_boost.get()) {
// first time called by this thread
// construct test element to be used in all subsequent calls from this thread
customMem::poolmanager_boost.reset(new pool);
}
customMem::poolmanager_boost->insert(a);
}
}
int size() { return customMem::poolmanager.size(); }
};
void func(bool localthread)
{
#pragma omp parallel for
for (int i = 0; i < 10000000; i++)
{
MVeryLongData a(localthread, i);
}
}
我想知道如果有,就是以函数func通过某种方式摆脱布尔localthread的方式合并poolmanager_boost和poolmanager。 有调用这两个变量的原因是thread_local在Visual Studio C++ 12中不完全支持。
如果可能或不可以,请让我(合并)。我可以使用std条件吗?
请注意,我试图避免模板。
编辑:由于我找不到解决方案,我想我没有选择,只能使用模板。所以带模板的解决方案也赞赏。就像一个getter函数,它返回一个指针boost_thread_ptr或thread_local池*。
那么你的运气了。这就是模板的用途。唯一的另一个解决方案是混乱的,并涉及宏。 – StoryTeller
我在避免使用模板,因为customMem已被用于许多其他位置。这意味着我也必须触摸它们。我对吗? –
是'bool localthread'编译时间常量? – StoryTeller