运行 使用变量类型 std::function 的线程

Running a thread using a variable type of std::function

我想在单独的线程中启动一个 std::function 类型。 我的代码目前看起来像这样:

struct bar
{
    std::function<void(int,int)> var;
};

struct foo
{
    bar* b;

    foo()
    {
        std::thread t(b->var); //Error attempt to use a deleted function
    }
};

为什么我会在这里尝试使用已删除的函数?

你的变量b->var是一个有两个参数的函数。您需要发送这些参数才能使其工作。

struct bar
{
  std::function<void(int,int)> var;
};

struct foo
{
   bar* b;
   foo()
   {
      std::thread t(b->var, 76, 89); // will call b->var(76, 89)
   }
};