condition.wait_for智能感知错误

condition.wait_for intellisense error

本文关键字:感知 错误 智能 for wait condition      更新时间:2023-10-16

我正在做一个基于线程的简单教程。在这个练习中,我应该让线程相互等待。我已经逐字复制了示例代码,除了我没有使用namespace std;而是每次需要时都用std::编写

有问题的错误对应于以下行:

if (condition.wait_for(std::unique_lock<std::mutex>(mut), std::chrono::seconds(3)))

Intellisense告诉我"表达式必须具有布尔类型(或可转换为布尔值)",但我查找了condition.wait_for的文档,它可以返回std::cv_status::timeoutstd::cv_status::no_timeout以及truefalse。当我去建造时,它认为它只能返回std::cv_status.

它应该可以工作吗?它是否需要第三个参数?我下面的示例没有使用。

正如Bo所说,该函数有两个版本。我将假设示例中有一个拼写错误,它意味着使用具有三个参数的函数版本,这是一些工作代码,我不知道它的工作方式是否与示例预期相同。

if (condition.wait_for(std::unique_lock<std::mutex>(mut), std::chrono::seconds(3), [] {return true; }))