【发布时间】:2013-12-04 12:21:16
【问题描述】:
我有一个跨平台(windows 和 unix+xcb)终端+graphics_window 应用程序,它大部分工作正常,直到您在输入提示符处等待太久,在重负载下图像可能会消失。 :(
我有一个用于解释器(postscript 解释器)的主循环(REPL),它每次在其循环周围调用一个事件处理函数。事件处理程序对通常是窗口的消息/事件循环执行一次迭代。但是输入是使用普通的 C i/o 处理的,因此当在 fgetc() 中阻塞时,事件处理程序永远不会被调用。
图形窗口仅用于输出。它没有按钮,只需要响应 Raise、Map、Expose 等事件。
如何安排在调用堆栈更深处的输入读取循环期间调用事件处理程序?这需要能够使用 POSIX 和 win32 API 来实现。
选项似乎是
- 非阻塞 i/o
在 unix 中相对简单。在 Windows 中看起来很痛苦 - 轮询
- 输入线程
pthreads? - 窗口线程
pthreads?
这些中的任何一个都可能比其他的痛苦少吗?
如果我可以留在 unix 上,那么这似乎可以解决问题:
#include <errno.h>
#include <stdio.h>
#include <termios.h>
#include <unistd.h>
#include <fcntl.h>
void idleproc () { /* simulates calling the event handler
(ie. one slice of the window loop) */
//printf("idle\n");
putchar('.');
}
int idlefgetc (FILE *stream) {
int ret;
do {
ret = fgetc(stream);
idleproc();
} while(ret == EOF &&
(errno == EAGAIN || errno == EINTR));
return ret;
}
int setraw (FILE *stream) {
struct termios tbuf;
if (tcgetattr(fileno(stream), &tbuf) == -1)
return -1;
tbuf.c_iflag &= ~(IGNBRK | BRKINT | PARMRK | ISTRIP
| INLCR | IGNCR | ICRNL | IXON);
tbuf.c_oflag &= ~OPOST;
tbuf.c_lflag &= ~(ECHO | ECHONL | ICANON | ISIG | IEXTEN);
tbuf.c_cflag &= ~(CSIZE | PARENB);
tbuf.c_cflag |= CS8;
if (tcsetattr(fileno(stream), TCSANOW, &tbuf) == -1)
return -1;
return 0;
}
int setnonblocking (FILE *stream) {
int flags;
if (setraw(stream) != 0)
return -1;
if (!((flags = fcntl(fileno(stream), F_GETFL)) & O_NONBLOCK)) {
flags |= O_NONBLOCK;
fcntl(fileno(stream), F_SETFL, flags);
}
return 0;
}
int main (int argc, char **argv) {
if (setnonblocking(stdin)) {
perror(argv[0]);
return 0;
}
printf("'%d'\n", idlefgetc(stdin));
system("stty sane");
return 0;
}
【问题讨论】:
-
GUI 应用程序通常有一个事件循环。如果您在终端上也有 REPL,那么两个循环应该在不同的线程中运行,具体取决于它们的交互方式。为了保持用户界面的响应性,GUI 通常不应与繁重的计算在同一线程中运行。
-
程序调用在 gui 窗口上绘图,但除此之外,窗口会自行处理。或者应该。
-
这段代码基于in my answer here中描述的在Linux上实现
getch的技术。
标签: c windows nonblocking stdio xcb