以下是我正在处理的图书馆的缩写:
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);
}
}
我想知道是否有办法通过某种方式合并poolmanager_boost和poolmanager来摆脱函数func中的 bool localthread 。 调用这两个变量的原因是visual studio c ++ 12中不完全支持thread_local。
如果可能,请告诉我(合并)。我可以使用std条件吗?
请注意我正在尝试避免使用模板。
编辑:由于我无法在我的问题上找到解决方案,我想我别无选择,只能使用模板。所以带模板的解决方案也很受欢迎。像getter函数一样返回一个指针boost_thread_ptr或thread_local pool *。
答案 0 :(得分:0)
你可以使用重载
struct localthread{};
struct nonlocaltchread{};
MVeryLongData(int a, localthread)
MVeryLongData(int a, nonlocaltchread)