[英]c++ condition_variable wait_for predicate in my class, std::thread <unresolved overloaded function type> error
I am trying to use a thread within my class, then the thread needs to use a condition_variable
, and the condition variable will be blocked until a predicate be changed to true
. 我试图在我的类中使用一个线程,然后线程需要使用
condition_variable
,并且条件变量将被阻塞,直到谓词被更改为true
。 The code looks like this: 代码如下所示:
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
}
} ;
This code on compilation throws an error saying: 编译时的这段代码会抛出错误说:
unresolved overloaded function type in the line “wait_for”.
“wait_for”行中未解决的重载函数类型。
Then i try modify it to: 然后我尝试将其修改为:
if(cv.wait_for(lck,std::chrono::milliseconds(3000),&myThreadClass::myPredicate))
But there is still an error. 但仍然存在错误。
The predicate needs to be callable without any context. 谓词需要在没有任何上下文的情况下可调用。 You are trying to invoke this on a non-static class member, which requires an object on which to operate.
您试图在非静态类成员上调用它,这需要一个操作对象。
You can use a lambda function to capture the object's context and wrap the function call into a suitable type: 您可以使用lambda函数捕获对象的上下文,并将函数调用包装为合适的类型:
const std::chrono::milliseconds timeout( 3000 );
if( cv.wait_for( lck, timeout, [this]{ return myPredicate(); } ) )
// ...
If you only created myPredicate
because of the condition variable, you can do away with it and just use this: 如果你因为条件变量而只创建了
myPredicate
,你可以取消它并使用它:
if( cv.wait_for( lck, timeout, [this]{ return bFlag; } ) )
A (pointer to a) member function as a predicate isn't suitable, since it requires an object to be called on, so you'll need a wrapper to bind the member function to an object and make it callable with just the two values to compare. 一个(指向一个)成员函数作为谓词的指针是不合适的,因为它需要一个对象被调用,所以你需要一个包装器将成员函数绑定到一个对象并使它只用两个值来调用它比较。
In C++11, you can bind a member function to an object: 在C ++ 11中,您可以将成员函数绑定到对象:
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),std::bind(&myThreadClass::myPredicate,this))) //something wrong?
cout<<"print something...1"<<endl;
else
cout<<"print something...2"<<endl;
}
}
void createThread(){
t = thread(&myThreadClass::myThreadFunction,this,10);//this is ok
}
};
you can use placeholders in bind. 你可以在bind中使用占位符。
Or you can use lambda functions. 或者你可以使用lambda函数。
Yes, in fact the best way to do this is.. 是的,事实上最好的方法是..
auto myPredicate = [this]{ return bFlag; }
and then using 然后使用
if (cv.wait_for(lck, std::chrono::milliseconds(3000), myPredicate))
This way you can eliminate the myPrediate
method. 这样就可以消除
myPrediate
方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.