title: POSIX AIO Library and Callback Handlers
body: <p>According to the documentation on aio_read/write, there are basically 2 ways that the AIO library can inform your application that an async file I/O operation has completed.  Either 1) you can use a signal, 2) you can use a callback function</p>
  
  <p>I think that callback functions are vastly preferable to signals, and would probably be much easier to integrate into higher-level multi-threaded libraries.  Unfortunately, the documentation for this functionality is a mess to say the least.  Some sources, such as the <a href="http://uw714doc.sco.com/en/man/html.5/sigevent.5.html" rel="noreferrer">man page for the sigevent struct</a>, indicate that you need to set the sigev_notify data member in the sigevent struct to SIGEV_CALLBACK and then provide a function handler.  Presumably, the handler is invoked in the same thread.  Other <a href="http://linux.die.net/man/3/mq_notify" rel="noreferrer">documentation</a> indicates you need to set sigev_notify to SIGEV_THREAD, which will invoke the callback handler in a newly created thread.</p>
  
  <p>In any case, on my Linux system (Ubuntu with a 2.6.28 kernel) SIGEV_CALLBACK doesn't seem to be defined anywhere, but SIGEV_THREAD works as advertised.  Unfortunately, creating a new thread to invoke the callback handler seems really inefficient, especially if you need to invoke many handlers.  It would be better to use an existing pool of threads, similar to the way most network I/O event demultiplexers work.  Some versions of UNIX, such as QNX, include a SIGEV_SIGNAL_THREAD flag, which allows you to invoke handlers using a specified existing thread, but this doesn't seem to be available on Linux, nor does it seem to even be a part of the POSIX standard.</p>
  
  <p>So, is it possible to use the POSIX AIO library in a way that invokes user handlers in a pre-allocated background thread/threadpool, rather than creating/destroying a new thread everytime a handler is invoked?</p>