c++ 编译错误:不能在此上下文中隐式捕获“this”

8wtpewkr  于 2023-02-17  发布在  其他
关注(0)|答案(1)|浏览(205)

我试图添加一个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";
        }
    
    }
ffx8fchx

ffx8fchx1#

您需要捕获this指针:

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

相关问题