-1

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
Aquarius_Girl
  • 21,790
  • 65
  • 230
  • 411
  • 1
    By capturing `[this, arg]` ? – Tony Tannous Aug 26 '20 at 19:31
  • Okay, you fixed *one* error. But how is this question different to the one you deleted? https://stackoverflow.com/questions/63603755/how-to-use-function-arguments-and-class-members-in-a-thread-in-c11 – StoryTeller - Unslander Monica Aug 26 '20 at 19:35
  • @storyteller I realise that I really did not understand the meaning of the error and how that solved in the other deleted question. – Aquarius_Girl Aug 26 '20 at 19:38
  • The solution of a good SO netizen to that is to edit your post, to emphasize that. You didn't. You haven't even added that detail to this repost. It looks virtually identical to the deleted question. – StoryTeller - Unslander Monica Aug 26 '20 at 19:40
  • @storyteller what is the way to do that? That question has been closed as a duplicate. If I edit it , it will require votes of 5 people for reopening. That will take ages. Kindly correct me if my understanding is wrong – Aquarius_Girl Aug 26 '20 at 19:43
  • It will take 3 people to re-open. Or one gold-badge holder. Either way, that is absolutely no excuse to repost. Flooding near identical questions to circumvent closure is not acting in good faith. – StoryTeller - Unslander Monica Aug 26 '20 at 19:45

2 Answers2

3

You have used counter without capturing it and note that you are capturing arg by value. By the following, you are capturing counter and arg by ref

void func( int arg )
{
    std::unique_lock< std::mutex > objUniqueLock( objMutex );
    objCondVar.wait( objUniqueLock, [&]{ return (arg == counter); } );

    std::cout << "\nnumber: " << numbers[counter];

    counter++;
}

If u want to capture them by value use = instead of & but I think this is unwanted.

asmmo
  • 6,922
  • 1
  • 11
  • 25
2

There is nothing specific to thread functionality about this -- the language requires that you capture this in the Lambda's capture list in order to use any member state of the surrounding class.

The code should be

        objCondVar.wait( objUniqueLock, [this, arg]{ return (arg == counter); } );
Human-Compiler
  • 11,022
  • 1
  • 32
  • 59