为什么Asio async_read_some回调从未调用过?

问题描述

// in another thread
void S::command(std::string&){
  asio::post(context_,[=](){
    //do something
    asio::post(context_,[=](){write();}); // if `write()` this line will work correctly 
  };
}

void S::write(std::string& str){
  std::cout<<"w";
  asio::async_write(socket,asio::buffer(str.data(),str.length()),[=](){write();}
  );
}

void S::read(){
  std::cout<<"r";
  socket_.async_read_some(asio::buffer(buf_,1024),[](){
       read(); // continue reading
    }
  );
}

如果执行command 1000次,将打印rrrwwwwwwwwwwww并停留在此处,什么也不打印。

功能command中,可以使用write()代替asio::post(...),打印rrwwrwrwrrrwwwrwrwrw...(1000r&w)

谁能告诉我为什么?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)