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:26:43
|
On 01/09/2020 09:43, lampahome wrote: > Hi, all > I'm a rookie to boost.asio. > > I write a program to write files asynchronously by boost.asio. But I'm > confused with some point. You're not alone. Even with the docs, the learning curve is steep at first. > 1. I need to add job to boost::asio::io_service and call > io_service.run(), then it will start running all jobs. Is there any > function automatically running newly jobs after I add job? It's hard to answer without knowing what you want to do. The service needs to believe it has a reason to live (jobs queued), otherwise it will quit. For example, you can use functions like io_service.post() to ask a different service to do something (e.g., you have two threads and one wants the other to do something). Or if you have only one thread (main), you can push some task that never finishes until its good and ready and then let your whole program happen in that loop. As long as it's event-based, it will work out. Is your program event-based or is it just that you have one file operation that needs to be async? > 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 DoWork() maybe you meant to write try { DoWork } catch(const std::exception& e) { std::err << "Well, that didn't go very well." << std::end; } -- Jeff Abrahamson +33 6 24 40 01 57 +44 7920 594 255 http://p27.eu/jeff/ http://transport-nantes.com/ |