Compilation error : 'this' cannot be implicitly captured in this context
Compilation error : 'this' cannot be implicitly captured in this context
我正在尝试添加 condition_variable 来处理线程,但在这一行出现编译错误:
this->cv.wait(lk, []{return this->ready;});
看起来变量 this->ready,'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;});
我正在尝试添加 condition_variable 来处理线程,但在这一行出现编译错误:
this->cv.wait(lk, []{return this->ready;});
看起来变量 this->ready,'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;});