Adam Rosenberg wrote:
On Fri, Nov 5, 2010 at 3:10 PM, Clemens Ladisch clemens@ladisch.de wrote:
And you should use poll() with all eight handles so that you don't eat CPU while waiting.)
I can't use poll because the application has to perform many other tasks in a deterministic manner (meaning I can only use threads and other processes to notify the main loop to perform some task).
As Mark wrote, this is what poll() was designed for.
the main loop of my application basically does this: while(1) { processNextAlsaStream(); processMp3Decoder(); processLCD(); processInputs(); processSerial(); }
With poll(), it would look somewhat like this:
struct pollfd pollfds[...]; // fill pollfds with all handles while (1) { poll(...); for (1..8) if (stream ready for writing) processAlsaStream(i); if (input ready for reading) processInputs(); if (serial ready for whatever) processSerial(); processMp3Decoder(); }
If you set the PCM device to non-blocking mode, you do not need to call avail_update before writing; just try to write as much as you currently have.
If you want to do something regularly, use the timeout of poll(), or use a timerfd.
You mentioned threads; these are not directly supported with poll() because they do not have a file handle, but if you want to wake up the main loop, you can write to an eventfd or to a pipe created with pipe().
Regards, Clemens