Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 决定编译时C+中的变量类型+;_C++_Boost Thread - Fatal编程技术网

C++ 决定编译时C+中的变量类型+;

C++ 决定编译时C+中的变量类型+;,c++,boost-thread,C++,Boost Thread,这是我正在工作的一个图书馆的缩图: 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

这是我正在工作的一个图书馆的缩图:

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);
    }
}
类池
{
私人:
std::向量m_缓冲区;
公众:
void insert(inta){m_buffer.push_back(a);}
int size(){return(int)(m_buffer.size());}
虚拟~pool(){}
}; 
类customMem
{
公众:
静态线程\u本地池池管理器;
静态boost::线程特定的\u ptrpoolmanager\u boost;
};
线程\本地池customMem::poolmanager;
boost::线程特定的\u ptrcustomMem::池管理器\u boost//很慢
MVeryLongData类:公共customMem
{
公众:
MVeryLongData(bool localthread,int a)
{
if(localthread)
{
customMem::poolmanager.insert(a);
}
其他的
{
如果(!customMem::poolmanager\u boost.get()){
//此线程第一次调用
//构造要在此线程的所有后续调用中使用的测试元素
customMem::poolmanager\u boost.reset(新池);
}
customMem::poolmanager\u boost->insert(a);
}
}
int size(){return customMem::poolmanager.size();}
};
void func(布尔本地线程)
{
#pragma-omp并行
对于(int i=0;i<10000000;i++)
{
MVeryLongData a(localthread,i);
}
}
我想知道是否有一种方法可以通过合并poolmanager\u boost和poolmanager来摆脱func函数中的bool localthread。 调用这两个变量的原因是VisualStudioC++ 12中TyRexLoad没有完全支持。p> 如果可能的话,请告诉我(合并)。我能用std吗

请注意,我试图避免使用模板


编辑:由于我无法找到问题的解决方案,我想我别无选择,只能使用模板。因此,使用模板的解决方案也很受欢迎。类似于一个getter函数,它返回一个boost\u thread\u ptr或thread\u local pool*的指针,您可以使用重载

   struct localthread{};
   struct nonlocaltchread{};

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

可以使用重载

   struct localthread{};
   struct nonlocaltchread{};

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

那你就不走运了。这就是模板的用途。唯一的解决方案是凌乱和涉及宏。我避免使用模板,因为customMem已经被用于许多其他位置。这意味着我也必须触摸它们。我说的对吗?
bool localthread
是编译时常量吗?@StoryTeller是的。它将是编译时常量。正如我在问题中提到的,双重声明的原因是不同的编译器。我不明白您希望如何在poolmanager和poolmanager_boost之间进行选择,否则对于模板或非模板解决方案,两者似乎都是可能的,定义一个带有虚拟插入方法的接口,该方法同时支持poolmanager和poolmanager_boost(或其后裔)Wild inherit应该在你运气不好的时候做这个把戏。这就是模板的作用。唯一的解决方案是凌乱的并且涉及宏。我避免使用模板,因为customMem已经在许多其他位置使用过。这意味着我也必须触摸它们。我说得对吗?是不是
bool localthread
编译时常量?@St是的。它将是编译时常量。正如我在问题中提到的,双重声明的原因是不同的编译器。我不知道您想如何在poolmanager和poolmanager_boost之间进行选择,否则对于模板还是非模板,这两种方法都是可能的,对于非模板解决方案,使用虚拟插入方法定义一个接口poolmanager和poolmanager_boost(或它的后代)都将继承,应该这样做