Intel® oneAPI DPC++/C++ Compiler Developer Guide and Reference

ID 767253
Date 3/22/2024
Public

A newer version of this document is available. Customers should click here to go to the newest version.

Document Table of Contents

Example for lio_listio Function

The following example illustrates how the lio_listio() function can be used.

// icx aio_sample6.c 
// aio_sample6.exe

#include <aio.h>
#include <stdio.h>
typedef struct aiocb  aiocb_t;
aiocb_t               my_aio;

#define IC_AIO_DATA_INIT(_aio, _fd, _dat, _len, _off)\
  {memset(&_aio, 0, sizeof(_aio)); \
   _aio.aio_fildes = _fd;          \
   _aio.aio_buf    = _dat;         \
   _aio.aio_nbytes = _len;         \
   _aio.aio_offset = _off;}

int main()
{
	static struct aiocb   aio[2];
	static struct aiocb  *aio_list[2] = {&aio[0], &aio[1]};
	int               i, ret;

	HANDLE fd = CreateFile("dat",
	   GENERIC_READ | GENERIC_WRITE,
	   FILE_SHARE_READ,
	   NULL,
	   OPEN_ALWAYS,
	   FILE_ATTRIBUTE_NORMAL,
	   NULL);

	/*
	** Data initialization and Synchronously writing
	*/
	IC_AIO_DATA_INIT(aio[0], fd, "rec#1\n", strlen("rec#1\n"), 0)
	IC_AIO_DATA_INIT(aio[1], fd, "rec#2\n", strlen("rec#2\n"),
	aio[0].aio_nbytes)
	aio[0].aio_lio_opcode = aio[1].aio_lio_opcode = LIO_WRITE;
	ret = lio_listio(LIO_WAIT, aio_list, 2, 0);
	if (ret) return ret;

	return 0;
}

Execution Result

>aio_sample6.exe
>type dat
rec#1
rec#2

The aio_lio_opcode refers to the field of each aiocb structure that specifies the operation to be performed. The supported operations are LIO_READ (do a read operation), LIO_WRITE (do a write operation), and LIO_NOP (do no operation); these symbols are defined in <aio.h>.

See Also