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

Compilation error : 'this' cannot be implicitly captured in this context

本文关键字:this 错误 上下文 编译      更新时间:2023-10-16

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

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

对于变量this->readythis似乎不在正确的范围内。

在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 datan";
        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 completedn";
        }
    
    }

您需要捕获this指针:

this->cv.wait(lk, [this]{return ready;});
相关文章: