私のクラス内のスレッドを使用しようとしているときにスレッドがcondition_variable
を使用する必要があり、条件変数がブロックされるまで述語はtrue
に変更されます。コードは次のようになります。ライン「wait_for」で私のクラスのstd :: thread <未解決のオーバーロードされた関数型>エラー
未解決のオーバーロードされた関数型を:コンパイルに
class myThreadClass{ bool bFlag; thread t ; mutex mtx; condition_variable cv; bool myPredicate(){ return bFlag; } int myThreadFunction(int arg){ while(true){ unique_lock<mutex> lck(mtx); if(cv.wait_for(lck,std::chrono::milliseconds(3000),myPredicate)) //something wrong? cout<<"print something...1"<<endl else cout<<"print something...2"<<endl } } void createThread(){ t = thread(&myThreadClass::myThreadFunction,this,10);//this is ok } } ;
このコードというエラーがスローされます。
その後、私はそれを修正してみてください。
if(cv.wait_for(lck,std::chrono::milliseconds(3000),&myThreadClass::myPredicate))
しかし、エラーが依然として存在します。
'(cv.wait_for(lck、std :: chrono :: milliseconds(3000)、[this] {return myPredicate();}))' – ildjarn
述語の使用が間違っています。それが述語が使えない方法です。 –