6
我想在這行中加入condition_variable處理線程,卻得到了一個編譯錯誤:編譯錯誤:「這個」不能在此背景下隱含捕獲
this->cv.wait(lk, []{return this->ready;});
貌似爲變量此 - >準備好了,'這'不在正確的範圍內。
在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";
}
}
只需閱讀文檔lambda表達式...或者閱讀編譯錯誤,對於這個問題! –