Search code examples
c++audioreal-time

Are virtual functions safe in real-time audio programming?


Real-time audio programming has particular constraints, due to the need to avoid audio glitches. Specifically, allocating and deallocating memory, or otherwise interacting with the operating system, should not be done in the audio thread.

When calling a virtual function, the program must find the relevant virtual table, lookup the pointer, and then call the function from the pointer. Is this process real-time safe?


Solution

  • Yes, it's fine. Virtual function dispatch is just like writing (*(obj->vtable[5]))(obj, args...). It doesn't involve any operations of unknown or possibly surprising complexity like allocating memory or I/O.