编译错误:在此上下文中无法隐式捕获"this"

Ray*_*Ray 12 c++ scope this condition-variable

我试图添加一个condition_variable来处理线程,但在这一行得到一个编译错误:

this->cv.wait(lk, []{return this->ready;});
Run Code Online (Sandbox Code Playgroud)

看起来像变量this-> ready,'this'不在合适的范围内.

在Java中,这可以通过TestThread来处理.这在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";
        }

    }
Run Code Online (Sandbox Code Playgroud)

m.s*_*.s. 31

你需要捕获this指针:

this->cv.wait(lk, [this]{return ready;});
Run Code Online (Sandbox Code Playgroud)