Re: [asio-users] Questions about file io when using boost.asio
Brought to you by:
chris_kohlhoff
From: Jeff A. <je...@p2...> - 2020-09-01 09:58:03
|
On 01/09/2020 11:46, lampahome wrote: > >> 2. If io_service.run() failed, is there any queue to let me know >> which job failed? Or it only jump exception? > > What do you mean by failed? The io_service probably didn't fail, > it's some task you asked it to do that failed. In that case, you > probably meant to catch the failure yourself. I.e., instead of > your task being > > My mistake. I mean the io operation failed. It seems read/write > handler can handle failed situations, but is there a centralized > queue(? or something) can collect these failed or all operations? The way I do this is that I call the dispatch functions with closures, so the function executing has the state it needs to do whatever it's going to do. And when I call a function with a completion handler, I again provide a closure so that it can do what it needs to do. One of the things a completion handler can do is check the result code of the operation. Now, I'll only class myself as "somewhere between newbie and expert but at neither extreme", so it's possible this conversation will uncover some problem with my error handling. > I want to do is like libaio in unix system and want to find some > library to reach that easier. Hmm, I'm on linux and yet I find libasio works quite well for me. Maybe I missed something. Although libasio gives me a bit more support, I think, than just libaio's completion handlers. > I have two threads(or one is main thread) and one thread will submit > io of file as more as it can, and another one thread will check the > execution state(i.e. return code) of each io. I thought this design > pattern will improve my program. I'd use completion handlers for checking return codes. Here's a snippet from a function of mine, in case it can help: void TcpConnection::SendMessage(...) { boost::asio::async_write( *sockets_[connection_index], boost::asio::buffer(send_queue_[connection_index].front()), [this, connection_index](boost::system::error_code ec, size_t length) { LOGGABLE; // This is the completion handler. if (ec) { LOG_WARNING << "Failed to send message: " << AsioError(ec); SendFailure(connection_index, ec); return; } // ... -- Jeff Abrahamson +33 6 24 40 01 57 +44 7920 594 255 http://p27.eu/jeff/ http://transport-nantes.com/ |