Pass parameters to std::thread wrapper

后端 未结 4 1428
一个人的身影
一个人的身影 2021-02-09 09:26

I want to implement a small thread wrapper that provides the information if a thread is still active, or if the thread has finished its work. For this I need to pass the functio

4条回答
  •  栀梦
    栀梦 (楼主)
    2021-02-09 10:10

    The answer by @O'Neil is correct, but I would like to offer a simple lambda approach, since you've tagged this as C++14.

    template
    ManagedThread::ManagedThread(Function&& f, Args&&... args):
          mActive(false),
          mThread([&] /*()*/ { // uncomment if C++11 compatibility needed
            mActive = true;
            std::forward(f)(std::forward(args)...);
            mActive = false;
          })
    {}
    

    This would eliminate the need for an external function all together.

提交回复
热议问题