Condition_variable项的计算结果不是一个带有0个参数的函数
condition_variable term does not evaluate to a function taking 0 arguments
我有一个问题使用等待与我的condition_variable和一个函数。我想让main 等待,直到池线程在继续执行程序之前完成它的所有任务。我想使用std::condition_variable
和isFinished()
池函数使main等待。我是这样做的:
//above are tasks being queued in the thread pool
{
std::unique_lock<std::mutex> lock(mainMut);
waitMain.wait(lock, pool.isFinished());
}
//I need to make sure the threads are done with calculations before moving on
和我的pool.isFinished()
//definition in ThreadPool class
bool isFinished();
//implementation
bool ThreadPool::isFinished()
{
{//aquire lock
std::unique_lock<std::mutex>
lock(queue_mutex);
if(tasks.empty())
return true;
else
return false;
}//free lock
}
但是我得到了错误
C:Program Files (x86)Microsoft Visual Studio 11.0VCincludecondition_variable(66): error C2064: term does not evaluate to a function taking 0 arguments
1> main.cpp(243) : see reference to function template instantiation 'void std::condition_variable::wait<bool>(std::unique_lock<_Mutex> &,_Predicate)' being compiled
1> with
1> [
1> _Mutex=std::mutex,
1> _Predicate=bool
1> ]
pool.isFinished()
被求值并返回bool
,但condition_variable::wait
需要一个函子,它可以反复调用该函子来确定何时停止等待。你可以使用std::bind
:
waitMain.wait(lock, std::bind(&ThreadPool::isFinished, &pool));
或lambda:
waitMain.wait(lock, [&]{ return pool.isFinished(); });
相关文章:
- 构造函数在退出函数时无法初始化一个参数
- 运算符重载:"operator+"必须采用零个或一个参数
- 如果需要转换,我可以在读取参数的同时将其移动到另一个参数吗?
- 两个函数模板候选项.将一个参数作为引用后,选择不太专业的模板
- 如何设置默认参数以防用户不输入另一个参数
- 使用一个参数的模板函数时出错(适用于 2)
- 仅当一个参数中未使用 std::function 时,模板函数替换才有效
- 如何在两个树通用的函数中创建一个参数?
- 使用带有一个参数函数的递归找到数字的平方
- 专门处理一个参数(C++模板)的两个模板参数
- 模板函数,其中一个参数需要专门化,而另一个不需要
- 如何更改参数包中的最后一个参数
- C++ 如何使用一个参数从派生类构造函数中调用具有两个参数的超类构造函数
- 模板参数重载,最后一个参数为非类型名
- 根据一个参数对数组进行排序
- 是否可以将指针传递给运算符,作为一个参数,例如指向函数的指针
- 用一个参数计数圆形链接列表中的节点
- 将数组作为一个参数从一个函数传递到C 中的另一个函数
- 将另一个参数传递给函子的库
- 分叉:使用一个参数运行 shell 时资源暂时不可用