Most examples of the select() function use a 0 to 'max file descriptor value' approach:

fd_set master_fd_set;
    FD_ZERO(&master_fd_set);

    fd_set temp_fd_set;
    FD_ZERO(&temp_fd_set);

    int max_fd = the_max_file_descriptor_value_of_descriptors_interested_in;

    ...

    temp_fd_set = master_fd_set;
    ::select(max_fd, &temp_fd_set, NULL, NULL, NULL);
    for (int i = 0; i <= max_fd; ++i)
    {
        if (FD_ISSET(i, &temp_fd_set))
        {
            // a read on file descriptor i
        }
        ...
    }

It seems to me that if I had an STL set (std::set<int>) of file descriptors, I could benefit from the set providing an ordering of the fd values (no need to manipulate the "max value" of 'max_fd'), and, instead of iterating from 0 to max_fd, I could just iterate over just those descriptors (in the set) in which I am interested:

for (set<int>::iterator iter = fdset.begin(); iter != fdset.end(); ++iter)
    {
        int i = *iter;
        if (FD_ISSET(i, &temp_fd_set))
        {
            // a read on file descriptor i
        }
        ...
    }

Does anyone know if it is required to iterate from 0 to max_fd when testing with FD_ISSET()? Would a "sparse" check of just those file descriptors (using a set iterator) in the set be ok to use?

Thanks for any input/insight.
-Mr68360

> Would a "sparse" check of just those file descriptors (using a set iterator) in the set be ok to use?

Absolutely OK.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.