locked
'this' was not captured for this lambda function RRS feed

  • Question

  • std::unique_lock<std::mutex> lk(mut);
    data_cond.wait(lk, []{return !data_queue.empty();});


    error at

    data_cond.wait(lk, []{return !data_queue.empty();});

    'this' was not captured for this lambda function

    where std::condition_variable data_cond;  std::queue<T> data_queue;


    Sigma : Every day has new and fresh idea and then search whether exists,
    Wednesday, October 12, 2011 9:40 AM

Answers

  • data_cond.wait(lk);
    極意
    • Marked as answer by 沈世鈞 Friday, November 4, 2011 8:57 AM
    Friday, November 4, 2011 8:57 AM

All replies

  • Seems pretty clear -- data_queue is a member of a class/struct object.   The lambda needs to know which object to operate on, however you didn't tell it, and you didn't allow it to capture the implicit this needed to access data_queue.  You can add a capture clause letting the compiler know you want to capture it though like

    // Disclaimer, I do Windows CE development, which doesn't 
    // have Lambda support yet, so I am doing this from memory
    // I might have the syntax wrong
    data_cond.wait(lk, [this]{return !data_queue.empty();});
    

    Of course, you need to make absolutely certain that your object will outlive the lambda in this case.

     

    Wednesday, October 12, 2011 1:50 PM
  • after tried, invalid type argument it's my first time to see this kind of writing, [this]{} what does this mean?
    Sigma : Every day has new and fresh idea and then search whether exists,
    Thursday, October 13, 2011 2:04 PM
  • It means you want the lambda to capture the "this" pointer.  Here is an example that might help.

    If you are unfamiliar with the concept of the "this" pointer, it is a pointer to the object whose member function you are calling.  For example,

     

    class Foo
    {
      public:
        int x;
        void function()
        {
            x = 3;          // these lines do the same thing
            this->x = 3; //
        }
    };
    

     

    A "this" pointer is available to every member function.  It is almost like it is an implicit first parameter passed to every member function.

    Foo f;

    f.function();  // how you normally call an object's member function

    In an alternate language that didn't have implicit "this" pointers, you might instead define things like:

    class Foo
    {
      public:
        int x;
        void function(Foo * const the_this_pointer)
        {
            the_this_pointer->x = 3;
        }
    };
    
    And call them like this:

    function(f);

     

     

    Thursday, October 13, 2011 2:41 PM
  • why invalid argument , data_cond.wait(lk, [this]{return !data_queue.empty();});?


    Sigma : Every day has new and fresh idea and then search whether exists,
    Friday, October 14, 2011 7:57 AM
  • data_cond.wait(lk);
    極意
    • Marked as answer by 沈世鈞 Friday, November 4, 2011 8:57 AM
    Friday, November 4, 2011 8:57 AM