According to the aio_read / write documentation, there are basically 2 ways that the AIO library can tell your application that the asynchronous file I / O operation has completed. Or 1) you can use the signal, 2) you can use the callback function
I think callback functions are much preferable to signals, and it would probably be much easier to integrate into higher-level multithreaded libraries. Unfortunately, the documentation for this functionality is at least a mess. Some sources, such as the man page for sigevent struct , indicate that you need to set the sigev_notify data member in sigevent struct to SIGEV_CALLBACK, and then provide a function handler. Presumably, the handler is called on the same thread. Other documentation indicates that you need to set sigev_notify to SIGEV_THREAD, which will output the callback handler to the newly created stream.
In any case, on my Linux system (Ubuntu with 2.6.28 kernel), SIGEV_CALLBACK does not seem to be defined anywhere, but SIGEV_THREAD works as advertised. Unfortunately, creating a new thread to call the callback handler seems really inefficient, especially if you need to call many handlers. It would be better to use an existing thread pool, similar to how demultiplexers work for work in an input-output network. Some versions of UNIX, such as QNX, have the SIGEV_SIGNAL_THREAD flag, which allows you to call handlers using the specified existing thread, but this does not seem to be available on Linux, and it does not seem to be part of the POSIX standard.
So, is it possible to use the POSIX AIO library in such a way as to call user handlers in a previously allocated background thread / threadpool, and not create / destroy a new thread each time the handler is called?
linux posix aio
Charles Salvia
source share