1
我很难在独立线程中调用类成员函数的正确语法。三种选择都不起作用。第1行和第2行引发编译时错误,第3行显示运行时错误。有谁可以告诉我什么是正确的方法。无法在使用C++ 11线程类的单独线程中调用类成员函数
#include <iostream>
#include <thread>
#include <mutex>
using namespace std;
struct Complex
{
mutex mx;
int i;
Complex(int q) : i(q) {}
void mul(int x)
{
lock_guard<mutex> lock(mx);
int z = i*x;
cout<<z<<endl;
}
void div(int x)
{
lock_guard<mutex> lock(mx);
int z = i/x;
cout<<z<<endl;
}
void both(int x, int y)
{
mul(x);
div(y);
}
};
int main()
{
//Complex complex(9);
//thread t(&Complex::both,&Complex(9),32, 23); --1
//thread t(&Complex::both,complex,32,23); --2
//thread t(&Complex::both,&complex,32,23); --3
return 0;
}
那么,这是完美的。谢谢您的回答 ! – user3798283
'std :: ref'版本取决于[最近解决的LWG问题](http://cplusplus.github.io/LWG/lwg-defects.html#2219),并且在某些实现中可能无法使用。 –
@ T.C。只是要删除它。没有意识到'ref'的东西太复杂了......谢谢! – Barry