0

How to declare a thread inside a class which run a member function? I tried several approaches according to online search : this

std::thread t(&(this->deQRequest));

this

std::thread t([this]{ deQRequest(); });

this

std::thread t(&this::deQRequest, this);

or

std::thread t(&this::deQRequest, *this);

None of them works.

Then I tried the following code, it works:

    std::thread spawn() {
        return std::move(
            std::thread([this] { this->deQRequest(); })
            );
    }

but my question is, why this

   std::thread t([this]{ deQRequest(); });

doesn't work? it always reminds an error: "Explicit type is missing, 'int' assumed" and "expected a declaration" .

My deQRequest function is a member function in the same class, my class looks like this:

  class sender{
      public:
          void deQRequest(){
             //some execution code
          };
      private:
        // here I try to declare a thread like this:std::thread t([this]{ deQRequest(); });
   }
strisunshine
  • 389
  • 2
  • 10
  • How did you declare `deQRequest`? Also have a look at [`std::bind()`](http://en.cppreference.com/w/cpp/utility/functional/bind). – πάντα ῥεῖ Apr 05 '15 at 12:28
  • possible duplicate of [Start thread with member function](http://stackoverflow.com/questions/10673585/start-thread-with-member-function) – Captain Giraffe Apr 05 '15 at 12:32
  • I already looked at this, the worked code come from there, but my question is different, I don't know why I can't declare the thread like normally I would do outside the class – strisunshine Apr 05 '15 at 12:34
  • @πάνταῥεῖ Yes, I tried "std::thread t((std::bind(&this::deQRequest, this)));" it doesn't work. The error is "expected a type specifier". I think in the first parameter I should refer to the class name instead of "this", but how to refer the class name inside the same class? – strisunshine Apr 05 '15 at 12:37
  • @πάνταῥεῖ I added the deQRequest code in the question post. :) – strisunshine Apr 05 '15 at 12:41
  • @strisunshine And it's a plain member function of which class please? What's `this` referring to in your context dude? – πάντα ῥεῖ Apr 05 '15 at 12:43
  • @πάνταῥεῖ like I said they are members of the same class, "this" refer to that class, added my class appearance in the post ' – strisunshine Apr 05 '15 at 12:47
  • @strisunshine In general we have the rule here, that you post a [MCVE](http://stackoverflow.com/help/mcve) for questions about erroneous/not working code. – πάντα ῥεῖ Apr 05 '15 at 13:06
  • If `std::thread t((std::bind(&this::deQRequest, this)));` doesn't compile, you should try to find out which expression there doesn't compile. You will find that it's not a problem related to threads or `std::bind()`. And, `this` does *not* refer to a class but to an object, in C++ those are distinct things. – Ulrich Eckhardt Apr 05 '15 at 13:50

1 Answers1

0

but my question is, why this

std::thread t([this]{ deQRequest(); });

doesn't work? it always reminds an error: "Explicit type is missing, 'int' assumed" and "expected a declaration".

It's not valid lambda function syntax. this is an implicit parameter of deQRequest, and can't be passed this way.

As from std::thread's constructor reference it takes a function parameter, and the arguments that should be passed there:

template< class Function, class... Args > 
explicit thread( Function&& f, Args&&... args );

Your class

 class sender{
 public:
    void deQRequest(){
        //some execution code
    };
 private:
    void foo() { // I just assume you're using some private function
        // here I try to declare a thread like 
        // this:std::thread t([this]{ deQRequest(); });
    }

    std::thread theThread; // I also assume you want to have a `std::thread`
                           // class member.
 }; // <<< Note the semicolon BTW

declares a member function, and you need to std::bind() that member function to a (your current) class instance:

    void foo() {
       theThread = std::thread(std::bind(&sender::deQRequest,this));
    }
πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190