微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

为什么在使用 std::bind 时没有调用析构函数

如何解决为什么在使用 std::bind 时没有调用析构函数

我有两个线程(io 和 otherIo 是 boost asio io_context 类型)。

  Thread 1 (798c)         |       Thread 2 (681c)
  ----------                      ---------
  io.run()                |       otherIo.run()

从第一个线程开始,我发布了一个事件,在该事件中我创建了一个名为“AnotherA”的对象 并运行属于 AnotherA 类的函数。 AnotherA 有一个私有变量 name_ 和一个名为 name() 的函数,它只打印其名称

io->post([=]()
    {
        AnotherA aobj(io,otherIo,"Kushina");
        aobj.anotherExecute();
    })

anotherExecute() 函数包含以下代码

void anotherExecute()
{
    name();
    otherIo_->post(std::bind(&AnotherA::name,this));
}


Output:
--------
2021-05-04 19:08:37 [798c]<info> Constructor called!!!
2021-05-04 19:08:37 [798c]<info> My name is : Kushina,address: 00FAEB00
2021-05-04 19:08:37 [798c]<info> Destructor called !!!
2021-05-04 19:08:37 [681c]<info> My name is : Kushina,address: 00FAEB00

我的问题是:

即使在调用析构函数之后,名称如何第二次打印?如果我选择的绑定重载复制了 AnotherA 对象(这解释了第一个问题中的第二个打印),为什么在 post 事件完成运行后没有调用析构函数。另外,为什么“this”的地址对于两者都是相同的。

名称函数

void name()
{
    log_info << "My name is : " << name_ << ",address: " << this;
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。