我试图添加一个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";
}
}
1条答案
按热度按时间ffx8fchx1#
您需要捕获
this
指针: