Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/153.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++ 如何在成员函数中使用std::thread调用其他成员函数?_C++_C++11 - Fatal编程技术网

C++ 如何在成员函数中使用std::thread调用其他成员函数?

C++ 如何在成员函数中使用std::thread调用其他成员函数?,c++,c++11,C++,C++11,我正在尝试使用线程调用我的一些成员函数。假设我有这个 class myclass { public: myclass(); double function1(); void function2(); }; myclass::myclass() {} double myclass::function1() { ... return a double; } void myclass::function2() { //use a thread

我正在尝试使用线程调用我的一些成员函数。假设我有这个

class myclass
{
public:
    myclass();
    double function1();
    void function2();
};

myclass::myclass()
{}


double myclass::function1()
{
    ...
    return a double;
}


void myclass::function2()
{
    //use a  thread to call function 1
    std::thread t(function1);//doesnt work!-wont compile
    std::thread t2(myclass::function1);//doesnt work either -wont compile
    std::thread t3(&myclass::function1);//doesnt work here either - wont compile
}

如何在C++中用其他成员函数中的线程调用成员函数? 顺便说一下,我正在使用Visual Studio 2013预览版

更新2:

我按照别人告诉我的那样做了,一些代码段现在编译得很好,而另一些则不行
这是生成错误的新示例代码:

class xGramManipulator
{
public:
    xGramManipulator();

    void ReadMonoGram();
    void ReadBiGram();

    void ReadMonoGram(double &);
    void ReadBiGram(double &);

    void CreateMonoGramAsync();
    void CreateBiGramAsync();
};

xGramManipulator::xGramManipulator()
{
}

void xGramManipulator::CreateMonoGramAsync()
{
    thread t(&xGramManipulator::ReadMonoGram, this);
}

void xGramManipulator::CreateBiGramAsync()
{
    thread t = thread(&xGramManipulator::ReadBiGram, this);
}
上述代码(这两个异步成员函数)生成以下错误:
错误消息:

错误C2661:'std::thread::thread':没有重载函数接受2个参数

std::thread(&myclass::function1,this)

如果需要消除重载歧义,则必须显式转换函数指针:

std::thread(static_cast<void (xGramManipulator::*)()>(&xGramManipulator::ReadMonoGram), this)
std::thread(static_cast(&xgrammaniperator::ReadMonoGram),这个)

尝试使用boost::bind(如下所述)绑定成员函数的隐式“this”参数:


这将使它成为一个没有参数的函数,您可以使用它来启动线程。

是什么阻止了您格式化问题?您已成为SO的活跃会员将近两年了。而且,“不起作用!”并不是对您的技术问题的有用描述。请做得更好。@LightnessRacesinOrbit:原因很简单,我只是不小心按了Enter键,问题就提交了,甚至在我尝试格式化它之前。虽然在提交后的第二天,它被“R.Martinho Fernandes”友好地格式化了。好的,这是一个很好的理由。谢谢。编辑后的代码示例对我来说很好。不过,请确保您将
#包括在内并使用
std::thread
。另外,请不要覆盖您的原始问题并更改它。你可以附加它,但现在你已经把所有的答案都弄得毫无意义了。好吧,包括在内!那么为什么不在VisualStudio2013预览版中编译呢?你在哪里编译的?Kerrek SB的答案更好,因为它更简单。它也是一个实际的答案,而不是一个超链接。@Lightness RaceSinorbit“它也是一个实际的答案,而不是一个超链接。”-但也没有真正解释任何东西,只是提供了“工作”代码。没有必要使用
boost::bind
std::bind
。@ChristianRau:你说得对。请停止回答明显的重复!天哪@光明赛道:我很年轻,需要一个代表。。。而且我并不总是足够清醒,不知道是否有重复:-S我通常只在我感觉强烈的特定区域发现重复,比如
eof()
,在这些区域,键入答案比关闭答案要长得多。@KerrekSB Nevermind,回答问题没有任何问题。如果结果是重复的,那不是你的错(而且答案不会伤害任何人)。@Hossein:对,我知道了-如果你有多个重载,你必须明确地消除函数指针的歧义。侯赛因:这只是对你想要的函数类型的转换。有关详细信息,请查阅成员函数类型。另一个重载的类型是
void(xgrammaniperator::*)(double)