When would you set the priority of a thread lower than the main thread's?


I could only find examples where the threads have a higher priority than the main thread. Could you give me an example where a thread would have a lower priority than the main thread ?


Hey @emeutier ,

I sometimes use threads with lower priority than main() for printing debug information. Let’s say main runs time-critical code (e.g delay measurements). Just adding a print in the main thread would completely mess up the measurements. Therefore, I just post print events to a lower priority thread, so debugging information is printed as soon as the main thread switches context (e.g by calling ztimer_sleep, mutex_lock, etc).

Hi. Depending on what you mean by “my main task”, I can say that you raise the priority of those tasks that are REAL TIME.

The main task might be only a manager and lower priority tasks might only handle not critical portions of your overall application (a heartbeat LED, reading a keyboard, outputing log or debugging info through a serial port, etc).

The documentation uses the expression “main thread”, and I understand that it corresponds to main(). It priority value is THREAD_PRIORITY_MAIN.

I couldn’t figure out how a thread with a priority value greater than THREAD_PRIORITY_MAIN (with a lower priority than the main thread) could ever run, but @jia200x’s post enlightens that.