簡體   English   中英

在編譯時C ++中決定變量類型

[英]decide variables types in compilation time 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);
    }
}

我想知道是否有辦法通過某種方式合並poolmanager_boost和poolmanager來擺脫函數func中的bool localthread。 調用這兩個變量的原因是visual studio c ++ 12中不完全支持thread_local。

如果可能,請告訴我(合並)。 我可以使用std條件嗎?

請注意,我試圖避免模板。

編輯 :由於我無法在我的問題上找到解決方案,我想我別無選擇,只能使用模板。 所以解決模板也很受歡迎。 類似於getter函數的東西,它返回一個指針boost_thread_ptr或thread_local pool *。

你可以使用重載

   struct localthread{};
   struct nonlocaltchread{};

   MVeryLongData(int a, localthread)
   MVeryLongData(int a, nonlocaltchread)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM