# Handling Errors Caused by Asynchronous I/O Functions

This topic only applies to Windows* OS.

The errno macro is used to obtain the errors that occur during asynchronous request functions such as aio_read(), aio_write(), aio_fsync(), and lio_listio() or asynchronous control functions, such as aio_cancel(), aio_error(), aio_return(), and aio_suspend().

The following example illustrates how errno can be used.

    #include <stdio.h>
    #include <stdlib.h>
    #include <aio.h>

    struct aiocb    my_aio;
    struct aiocb   *my_aio_list[1] = {&my_aio};

    int main()
    {
      int   res;
      double  arr[123456];
     timespec_t   my_t = {1, 0};

    /* Data initialization */
     my_aio.aio_fildes = CreateFile("dat",
       GENERIC_READ | GENERIC_WRITE,
       FILE_SHARE_READ,
       NULL,
       OPEN_ALWAYS,
       FILE_ATTRIBUTE_NORMAL,
       NULL);
     my_aio.aio_buf    = (volatile char *)arr;
     my_aio.aio_nbytes = sizeof(arr);

    /* Do asynchronous writing with computation overlapping */
     aio_write(&my_aio);
     do_compute(arr, 123456);

    /* Suspend the asynchronous writing for 1 sec */
     res = aio_suspend(my_aio_list, 1, &my_t);
     if ( res ) {

    /* The call was ended by timeout, before the indicated operations had completed. */
      if ( errno == EAGAIN ) {
      res = aio_suspend(my_aio_list, 1, 0);
      if ( res ) {
      printf("aio_suspend returned non-0\n"); return errno;}
      }
      else
      if ( res ) {
      printf("aio_suspend returned neither 0 nor EAGAIN\n");
      return errno;
      }
     }

     CloseHandle(my_aio.aio_fildes);
     printf("\nPass\n");

     return 0;
    }

In the example, the program executes an asynchronous write operation, using aio_write(), overlapping with some computation, the do_compute() function execution. The pending write operation is suspended for one second using aio_suspend().

On successful execution of the asynchronous write operation, zero is returned. `EAGAIN` or any other error value is returned when the call is ended by timeout before the indicated operation has completed.

You can check `EAGAIN` using the errno macro.

Parent topic: Intel's C++ Asynchronous I/O Library for Windows* Operating Systems