2016-11-23 16 views
1

に変数の型を決定し、私が働いているライブラリの収縮です:何とかによって関数funcにブールlocalthreadを取り除くための方法がある場合、私は疑問に思ってはここで、コンパイル時間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をマージします。 これらの2つの変数を呼び出す理由は、thread_localがVisual StudioのC++ 12で完全にサポートされていないことです。

可能かどうか(マージ)できたら教えてください。条件付きで標準を使うことはできますか?

私はテンプレートを避けようとしています。

EDIT:私の質問で解決策を見つけることができなかったので、テンプレートを使用する以外に選択肢はありません。したがって、テンプレート付きのソリューションは、と同様に評価されています。 boost_thread_ptrまたはthread_localプール*のいずれかのポインタを返すgetter関数のようなものです。

+2

の過負荷を使用することができます。これがテンプレートの目的です。唯一の他の解決策は、乱雑でマクロを必要とします。 – StoryTeller

+0

私はテンプレートを避けています。なぜなら、customMemは他の多くの位置で使われていたからです。つまり、私もそれらに触れなければならないということです。私は正しい? –

+0

は、 'bool localthread'はコンパイル時定数ですか? – StoryTeller

答えて

0

あなたは、あなたは運の外にある

struct localthread{}; 
    struct nonlocaltchread{}; 

    MVeryLongData(int a, localthread) 
    MVeryLongData(int a, nonlocaltchread) 
関連する問題