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