简体   繁体   中英

How do you read from a ifstream asynchronously in C++?

The ifstream is reading from a UNIX pipe, currently the communication is done by sending a signal to the reading application, but I would like to get rid of the signals and use proper async io.

In C I would use the select() function with a timeout, so I could react immediately when there is something to read, and waith for IO otherwise.

In C++ I can't access the fd of the file, so I can't use select(). What's the most reasonable approach to doing this? Do I have to use some sort of Boost library, or can this be done using the standard C++ libraries?

std::ifstream does not support asynchronous operations, it is designed for synchronous IO. You could build async IO around it using additional threads and IO queue, but in general this is not a good idea for efficiency reasons.

For asynchronous IO you might want to look at Boost.ASIO , in particular its stream_descriptor , which can be used to adopt arbitrary stream file descriptors like pipes.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM