2017-04-05 6 views
1

内の引数を持つメンバ関数を起動する私は、メンバ関数MyClass::doStuff(QString & str)別のスレッド

を持っている私はこのようなスレッドからその関数を呼び出すようにしようとしています:

std::thread thr(&MyClass::doStuff, this); 

しかし、それはエラーになります:

/usr/include/c++/4.8.2/functional:1697: error: no type named ‘type’ in ‘class std::result_of<std::_Mem_fn<void (MyClass::*)(QString&)>(MyClass*)>’ typedef typename result_of<_Callable(_Args...)>::type result_type;

だから私はそれを引数を与えることを試みてきました:

QString a("test"); 
std::thread thr(&MyClass::doStuff(a), this); 

しかし、その本エラーで結果:

がどのように私は別のスレッドから、引数を指定してそのメンバ関数を実行するに行くかlvalue required as unary ‘&’ operand

答えて

1

ただ、スレッドのコンストラクタに引数を追加します。

MyClass::doStuff(QString& str) { /* ... */ } 

// ... 

QString a("test"); 
std::thread thr(&MyClass::doStuff, this, std::ref(a)); // wrap references 
+0

質問:あなたの関数として

QString a("test"); std::thread thr(&MyClass::doStuff, this, a); 

は、あなたがこのようなstd::ref()を使用する必要があります参照受け入れdoStuff'が求める 'ので、それは別のスレッドで実行されていますが、 'a'を削除することを止めるものは何もなく、あなたはUBになりますか? – Ceros

+0

@Cerosもちろん、変数を削除すると別のものが使用されている場合は問題あります:) – Galik

+0

この場合、 'std :: ref'の目的は何ですか? – Ceros

関連する問題