gpt4 book ai didi

c++ - data_condition 等待无效参数错误

转载 作者:行者123 更新时间:2023-11-28 03:45:38 25 4
gpt4 key购买 nike

data_cond.wait(lk, [this]{return !data_queue.empty();});

编译后参数无效

g++ -std=c++0x -Wall -pthread threadpool.cc -o hello 

最初来自书本是

data_cond.wait(lk, []{return !data_queue.empty();});

最佳答案

这看起来很像我书中的代码:C++ Concurrency in Action ,尤其是缺少[this]捕获(一个打字错误,将在最终打印中修复)。

不幸的是,在 g++ 4.5 和 4.6 中存在一个错误,编译器无法处理带有 [this] 的 lambda。在类模板中捕获。这甚至适用于简单的模板。您可以在下面的简单类中看到问题:

struct X
{
int i;
void foo() {
[this] { ++i; };
}
};

template<typename T>
struct Y
{
T i;
void foo() {
[this] { ++i; };
}
};

g++ 4.5 和 g++ 4.6 都会在 Y::foo 中的 lambda 上给出“无效类型参数”错误, 但愉快地接受 X::foo 中的相同代码.

希望这会在 g++ 的 future 版本中得到修复。同时,我建议在 wait 周围使用显式调用:

while(data_queue.empty())
{
data_cond.wait(lk);
}

关于c++ - data_condition 等待无效参数错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7775537/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com