Search code examples
selectposix

select but without wait (POSIX)


I have the typical setup for file io which works well with select like:

int retval = select(maxfd +1 , &read_set, &write_set, &error_set, 0);   // timeout==0 -> endless

But now I have a situation where I want to loop and check on every cycle if one of the file selectors become ready. I do not want to start a separate thread for that! Is there something in posix/linux which can be used, hopefully with the same FD_SET like data structures which checks for file state without waiting for them?

Yes, I can set timeout for select to a minimal value, but I hope it can be done without that.


Solution

  • POSIX says:

    To effect a poll, the timeout parameter should not be a null pointer, and should point to a zero-valued timespec structure.

    So for your application, it should be sufficient to call select like this:

    struct timeval zero = { 0, 0 };
    int retval = select(maxfd +1 , &read_set, &write_set, &error_set, &zero);