为什么在使用 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 (将#修改为@)