Чтобы определить, статично ли свойство, необходимо получить MethodInfo для того, чтобы получать или установить средство доступа, путем вызова GetGetMethod или метода GetSetMethod, и исследовать его свойство IsStatic.
http://msdn.microsoft.com/en-us/library/system.reflection.propertyinfo.aspx
select() does exactly what you want: signal that an operation (read, in this case) on a file descriptor (file, socket, whatever) will not block.
#include <stdio.h>
#include <sys/select.h>
int is_ready(int fd) {
fd_set fdset;
struct timeval timeout;
int ret;
FD_ZERO(&fdset);
FD_SET(fd, &fdset);
timeout.tv_sec = 0;
timeout.tv_usec = 1;
//int select(int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds,
struct timeval *timeout);
return select(fd+1, &fdset, NULL, NULL, &timeout) == 1 ? 1 : 0;
}
You can now check a file descriptor before use, for instance in order to empty the file descriptor:
void empty_fd(int fd) {
char buffer[1024];
while (is_ready(fd)) {
read(fd, buffer, sizeof(buffer));
}
}
In your case, use fileno(stdin) to get the file descriptor of stdin:
if (is_ready(fileno(stdin))) {
/* read stuff from stdin will not block */
}
Я не уверен, можно ли установить O_NONBLOCK на стандартный ввод, но select ()
или poll ()
определенно выполнят свою работу.
Да, вы можете использовать выберите
(с нулевым таймаутом). Вам не нужно устанавливать неблокирующий дескриптор файла - если select
сообщает вам, что файловый дескриптор доступен для чтения, то чтение
на нем определенно не будет блокироваться.
Итак, опрашивайте дескриптор 0 файла иногда с помощью select
, и, если он доступен для чтения, считывает
его. Если read
возвращает 0, значит, он закрыт.