Start thread with member function

前端 未结 5 1339
抹茶落季
抹茶落季 2020-11-21 04:31

I am trying to construct a std::thread with a member function that takes no arguments and returns void. I can\'t figure out any syntax that works -

5条回答
  •  野趣味
    野趣味 (楼主)
    2020-11-21 05:13

    Since you are using C++11, lambda-expression is a nice&clean solution.

    class blub {
        void test() {}
      public:
        std::thread spawn() {
          return std::thread( [this] { this->test(); } );
        }
    };
    

    since this-> can be omitted, it could be shorten to:

    std::thread( [this] { test(); } )
    

    or just (deprecated)

    std::thread( [=] { test(); } )

提交回复
热议问题