I'm writing some classes that use AuxTask internally. Can you elaborate the difference between AuxTaskNonRT and AuxTaskRT ?

I understand AuxTaskNonRT pretty well, but how does the AuxTaskRT work.

  • What is the clock ?
  • Is it locked to run the scheduled function once on each call to render ?

    HjalteBestedMoeller What is the clock ?

    what clock?

    HjalteBestedMoeller Is it locked to run the scheduled function once on each call to render ?

    no.

    Both AuxTask... classes do basically the same: they create a thread which blocks reading from a pipe. Other threads can then send to that pipe a typed message. After a message is sent, the thread will wake up as soon as possible (depending on priority) and process the incoming data. As a particular case, you can use the version were no message is passed, which will result simply in the thread being scheduled without data to process, useful in some circumstances.

    The difference between AuxTaskRT and AuxTaskNonRT is in the type of inter-thread communication.
    AuxTaskRT uses a Xenomai message queue (basically the Xenomai-friendly version of pthread's mq_*). This allows the created thread (which is a Xenomai thread) to receive messages and being scheduled without ever leaving primary (Xenomai) mode. This is more efficient if the thread is performing some RT-critical task and would not leave primary mode. For instance, a high-priority DSP co-routine could use this.
    AuxTaskNonRT uses a Xenomai pipe (as seen in the Pipe library), which keeps the created thread in secondary (Linux mode). This is more efficient if the thread is performing non-RT tasks which use Linux services (e.g.: disk, USB, network), as it would have to switch to secondary (Linux) mode to use those anyhow.

    Mode switches (from primary to secondary or viceversa) are CPU intensive (you waste some 20-40us for each) and are best avoided especially if repeated thousands of times per second. Therefore, the choice of AuxTaskRT vs AuxTaskNonRT depends on what type of activities are performed by your thread. Ultimately, if your task only runs occasionally, the performance would be mostly identical in both cases.

    These two classes are not well engineered, which is why we tend not to publicize them much. Some issues:
    - they should be templated or subclassed instead of having three different constructors, three different create() methods and three different schedule() methods just to allow for three different types of messages to be passed.
    - there is probably plenty of duplicated code around
    - they both create a Xenomai thread, whereas only the RT one should strictly do that
    - they don't allow to set the length of the pipe/message queue
    - AuxTaskNonRT should probably use the Pipe library instead of reimplementing it
    - the depend on Bela.h and BELA_AUDIO_PRIORITY without a good reason

      7 days later

      giuliomoro Thank you for the detailed explanation.

      I have a class with a function that is called by the realtime thread. From that function I want to call another callable but in a NonRT thread.

      What is the best/simplest way to achieve that ?

      Use AuxTaskNonRT, register a callback with a suitable type of argument among the ones provided, then call task.schedule(...) from the RT thread (with appropriate argument) when you want to run it.
      See for instance how it's been used in OscSender.cpp.