My case is specific to threads:
#include <vector>
#include <thread>
#include <mutex>
#include <condition_variable>
#include <iostream>
class A
{
public:
std::vector<std::thread> threadVector;
std::vector<int> numbers;
std::mutex objMutex;
std::condition_variable objCondVar;
int counter;
void func( int arg )
{
std::unique_lock< std::mutex > objUniqueLock( objMutex );
objCondVar.wait( objUniqueLock, [arg]{ return (arg == counter); } );
std::cout << "\nnumber: " << numbers[counter];
counter++;
}
A()
{
counter = 0;
numbers.push_back(10);
numbers.push_back(20);
numbers.push_back(30);
threadVector.push_back( std::thread( &A::func, this, 1 ));
threadVector.push_back( std::thread( &A::func, this, 2 ));
threadVector.push_back( std::thread( &A::func, this, 3 ));
}
};
int main()
{
A a;
}
I saw this thread lambdas require capturing 'this' to call static member function?
But I think this case is specific to thread function whereas that is a general function case.
What is the way to access the class member counter
in the lambda function?
error: ‘this’ was not captured for this lambda function