简体   繁体   English

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

[英]Running a thread using a variable type of std::function

I would like to launch a std::function type in a separate thread. 我想在一个单独的线程中启动一个std :: function类型。 My code currently looks like this : 我的代码目前看起来像这样:

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
    }
};

Why am I getting attempt to use a deleted function here ? 为什么我在这里尝试使用已删除的功能?

Your variable b->var is a function that takes two parameters. 你的变量b->var是一个带两个参数的函数。 You need to send these parameters to make it work. 您需要发送这些参数才能使其正常工作。

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)
   }
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM