Search code examples
cmultithreadinginterrupt-handling

C printf() in interrupt handler?


I heard printf() in C is not supposed to be used in ISR. Is it because it's a blocking call, or is it because it's not re-entrant?

If printf() is not re-entrant, then wouldn't it means that it can not be used for multi-thread program as well, unless it's 'synchronized' in some way?


Solution

  • I think it might be all of those, and more. Typical printf() implementations can do dynamic (heap) memory allocation, which is generally not the fastest thing to be doing, and might also have issues with being non-re-entrant. The fastness thing can matter since you're typically not supposed to spend too much time in an interrupt service routine.

    See this answer for a discussion about printf() and malloc().