C++ 将具有构造函数的类的成员函数传递给std::thread

C++ 将具有构造函数的类的成员函数传递给std::thread,c++,multithreading,c++11,C++,Multithreading,C++11,我在一个类中定义了一个函数,该类使用一系列构造函数,并试图将一个函数(fillgamma)传递给std::thread。构造函数本身非常大,所以我不能每次使用它时都将它们作为参数传递给函数。当我试图编译时,我得到了一个C3867错误,我很确定这是由于标记为bad line的行引起的 我的项目已经变得相当大了,所以我省略了大量的外围设备,但下面是类定义: class gamma_threads { public: void fillgamma(int t, float &total

我在一个类中定义了一个函数,该类使用一系列构造函数,并试图将一个函数(
fillgamma
)传递给
std::thread
。构造函数本身非常大,所以我不能每次使用它时都将它们作为参数传递给函数。当我试图编译时,我得到了一个C3867错误,我很确定这是由于标记为bad line的行引起的

我的项目已经变得相当大了,所以我省略了大量的外围设备,但下面是类定义:

class gamma_threads
{
public:
    void fillgamma(int t, float &total);

    gamma_threads(std::vector<float> delpro, float Stau,float gamma0, int N);
    ~gamma_threads();

private:
    std::vector<float> delpro;
    const float Stau, gamma0;
    float gint=0;
    const int N;

};
class gamma_线程
{
公众:
空隙填充伽马(整数t、浮动和总计);
gamma_线程(std::vector delpro、浮点Stau、浮点gamma0、int N);
~gamma_线程();
私人:
std::vector delpro;
常量浮点Stau,gamma0;
浮动gint=0;
const int N;
};
下面是试图实现线程的代码主体:

gamma_threads T(delpro, Stau, gammafbb[0], N); // vector, float, float, int

while (flag==1)
{
    std::thread th[8];
    for (int i = 0; i < 8; i++)
    { 
        if (!tlist.empty())  //tlist is a queue where I store the values to be fed to the threads
        {
            int temp_t = tlist.front();

            th[i] = std::thread((T.fillgamma, temp_t, gammafbb[temp_t])); // bad line
            if (temp_t + 8 <= tmax){ tlist.push(temp_t + 8); }
            else {  flag == 0; }
        }
    }
    for (int i = 0; i < 8; i++)
    {
        th[i].join();
    }
}
gamma_线程T(delpro、Stau、gammafbb[0],N);//向量,浮点,浮点,整数
while(标志==1)
{
标准:螺纹th[8];
对于(int i=0;i<8;i++)
{ 
如果(!tlist.empty())//tlist是一个队列,我在其中存储要提供给线程的值
{
int temp_t=tlist.front();
th[i]=std::thread((T.fillgamma,temp\u T,gammafbb[temp\u T]);//错误行

如果(temp_t+8)“坏线”有双括号,请使用一对括号:
th[i]=std::thread(t.fillgamma,temp_t,gammafbb[temp_t])
指向成员的指针是使用
&class\u name::member\u name
语法形成的。您还需要单独传递对象,作为第一个参数。此外,
std::thread
的构造函数复制其参数,因此您需要用
std::ref
(对于对象,您需要传递它的地址)。
const
类成员通常也不是一个好主意。
std::thread(&T::fillgamma,&T,temp\T,std::ref(gammafbb[temp\u T])
,但老实说,您的代码读起来有点象形文字,所以这只是一个暗中的尝试。或者使用lambda:{th[i]=std::thread([T,&temp_T,&gammafbb](){T.fillgamma(temp_T,gammafbb[temp_T]);}