本文介绍了boost :: condition_variable-将wait_for与谓词一起使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想等待一段时间.我阅读了boost文档,似乎最好将函数 wait_for 与谓词一起使用,如此处.

I would like to wait for a condition for a certain amount of time.I read the boost documentation and it seems that it is preferable to use the function wait_for with a predicate, as described here.

不幸的是,这个例子对我来说并没有太大用处.我应该如何写谓词?我试图编写上面报告的代码,但是Visual Studio编译器抱怨: c:\ boost \ boost \ thread \ win32 \ condition_variable.hpp(394):错误C2064:术语未求值为带有0个参数的函数

Unfortunately the example is not really useful for me. How should I write the predicate? I tried to write the code reported above but the visual studio compiler is complaining: c:\boost\boost\thread\win32\condition_variable.hpp(394): error C2064: term does not evaluate to a function taking 0 arguments

这是代码的一部分:

class MyClass{

  boost::mutex mutex;
  boost::condition_variable myCondition;
  //...
  void foo();
  bool myPredicate();
}


void MyClass::foo(){

  boost::unique_lock<boost::mutex> lock(mutex);

  boost::chrono::microseconds period(25000);
  // ...
  boost::chrono::system_clock::time_point wakeUpTime = boost::chrono::system_clock::now() + period;
  if(myCondition.wait_until(lock,wakeUpTime,MyClass::myPredicate) == true){/...}

}

bool MyClass::myPredicate(){

  if(...)
    return true;
  else
    return true;
}

wait_for 与谓词一起使用的正确方法是什么?

What's the correct way of using wait_for with predicate?

推荐答案

建议将 wait 函数与谓词一起使用,因为与手写循环相比,它们不易出错.手写循环可能如下所示:

It's recommended to use the wait functions with predicate, because they are less error-prone compared to hand-written loops. A hand-written loop might look as follows:

for (;;) {
    if (myPredicate()) {
        // ... [successful case]
        break;
    } else if (myCondition.wait_until(lock, wakeUpTime) == boost::cv_status::timeout) {
        // ... [timeout case]
        break;
    } // else: continue loop [spurious wakeup]
}

如果将谓词传递给 wait 函数,则可能是类似函数的事物,可以不带任何参数地调用它并返回一个类型,该类型可以用作 bool.例如,您可以为此目的使用 static 成员函数:

If you pass a predicate to the wait function, it might be a function-like thing that can be invoked with no arguments and returns a type, that can be used as a bool. For example you can use a static member function for that purpose:

struct Foobar {
    static bool isFoobar();
};

myCondition.wait_until(lock, wakeUpTime, Foobar::isFoobar);

您不能直接传递非静态成员函数,因为它只能与对象一起调用.但是,您可以改用功能对象:

You can't directly pass a non-static member function, because it can only be invoked with an object. However, you can use a function-object instead:

struct MyPredicateWrapper {
    MyClass* _ptr;
    explicit MyPredicateWrapper(MyClass* ptr) : _ptr(ptr) { }
    bool operator()() const { return _ptr->myPredicate(); }
};

myCondition.wait_until(lock, wakeUpTime, MyPredicateWrapper(this));

您可以使用 boost :: bind :

myCondition.wait_until(lock, wakeUpTime, boost::bind(&MyClass::myPredicate, this));

如果使用的是C ++ 11,还可以使用 lambda函数:

And if you are using C++11, you can also use a lambda function:

myCondition.wait_until(lock, wakeUpTime, [this] { return myPredicate(); });

这篇关于boost :: condition_variable-将wait_for与谓词一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 11:34