I've written a program in C which has a main loop that looks something like this:
while (running) {
int recv = read_line(fd1, buf, sizeof(buf));
if (recv > -1) {
processCommand(buf);
}
while (XPending(display)) {
XNextEvent(display, &ev); // This is blocking
// Handle event
}
}
You can see that we have 2 event sources, one of which is blocking.
This works fine but it means I have 100% CPU usage. Normally in a program like this, the blocking call would take care of that but because I have 2 things to check, I can't allow the loop to be blocked.
What's the correct way to program this such that:
As mentioned before, 'select' or 'poll' are your friends. To get the file descriptor of your display:
ConnectionNumber(display)
int XConnectionNumber(Display *display);
Both return a connection number for the specified display. On a POSIX-conformant system, this is the file descriptor of the connection.