【发布时间】:2018-11-26 16:16:52
【问题描述】:
我有异步 tcp 服务器,它从客户端读取异步消息。我应该对我推荐的按摩进行一些操作,然后将答案发送给客户。操作应该是异步的,并且在操作完成后,服务器应该向客户端发送应答。
我想做下一个:
//...
std::string answer;
boost::asio::io_service &io_service_;
void do_read(){
//async read from client and call on_read()
}
void on_read(std::string msg){
//here I have a problem - I don't know, how to call func() asynchronous
//I want to do something like this:
RunAcync(boost::bind(&CClientSession::func, shared_from_this(), msg)
, boost::bind(&CClientSession::on_func_executed, shared_from_this())
,io_service_ );
do_read();
}
void func(std::string msg) {
//do long work hare with msg
answer = msg;
}
void on_func_executed(){
do_write(answer);
}
void do_write(std::string msg){
//async write to client and call on_write()
}
void on_write(){
do_read();
}
//...
所以我的 func 应该在与 io_service 相同的线程下执行。
PS:: 这是类的一部分,适用于客户端
【问题讨论】:
标签: c++ multithreading boost-asio