[alsa-devel] ALSA processor usage is too high

Clemens Ladisch clemens at ladisch.de
Mon Nov 8 11:39:53 CET 2010

Adam Rosenberg wrote:
> On Fri, Nov 5, 2010 at 3:10 PM, Clemens Ladisch <clemens at 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)
     for (1..8)
       if (stream ready for writing)
     if (input ready for reading)
     if (serial ready for whatever)

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

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().


More information about the Alsa-devel mailing list