gpt4 book ai didi

c++ - 编译错误 : 'this' cannot be implicitly captured in this context

转载 作者:可可西里 更新时间:2023-11-01 14:54:07 30 4
gpt4 key购买 nike

我正在尝试添加一个条件变量来处理线程,但在这一行出现编译错误:

this->cv.wait(lk, []{return this->ready;});

看起来变量 this-> 准备好了,'this' 不在正确的范围内。

在 Java 中,这可以用 TestThread.this 处理,C++ 中是否有任何东西可以做同样的事情?

void TestThread::Thread_Activity()
{
std::cout << "Thread started \n";
// Wait until ThreadA() sends data
{
std::unique_lock<std::mutex> lk(m);
this->cv.wait(lk, []{return ready;});
}

std::cout << "Thread is processing data\n";
data += " after processing";
// Send data back to ThreadA through the condition variable
{
// std::lock_guard<std::mutex> lk(m);
processed = true;
// std::cout << "Thread B signals data processing completed\n";
}

}

最佳答案

您需要捕获this 指针:

this->cv.wait(lk, [this]{return ready;});

关于c++ - 编译错误 : 'this' cannot be implicitly captured in this context,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38595834/

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