mike3 said:
Hi.
Busy-waiting is a known anti-pattern that should be avoided. However,
in C,
there is no standard alternative, so when a wait is required and it's
not busy,
the program becomes 100% non-portable. So then why not include this
type
of non-busy wait functionality in the standard?
C++ provides a member function named in_avail():
template <class Ch, class Tr= char_traits<Ch> >
class basic_streambuf{
public:
// ...
streamsize in_avail(); // is input ready?
// ...
};
Perhaps C can adopt something like this.
TC++PL3 mentions on page 647:
"A call to in_avail() is used to see how many characters are available
in the buffer. This can be used to avoid waiting for input. When reading
from a stream connected to a keyboard, cin.get(c) might wait until the
user comes back from lunch. On some systems and for some applications,
it can be worthwhile taking that into account when reading. For example:
if(cin.rdbuf()->in_avail()) { // get() will not block
cin.get(c);
// do something
}
else { // get() might block
// do something else
}
Note that on some systems, it can be hard to determine if input is
available. Thus, in_avail() might be (poorly) implemented to return 0 in
cases where an input operation would succeed".