Difference between task and callback? (Task vs Callback)

Post Reply
Posts: 1
Joined: Mon Jan 11, 2021 11:51 am

Difference between task and callback? (Task vs Callback)

Post by avitalm »

As I understand a task is something that is like a thread and is called once while a callback is something that is called every X number of seconds or every time an event occurs. My question is what are the differences in the way they are implemented and when should you use one instead of the other.
Posts: 2392
Joined: Tue Feb 06, 2007 12:36 pm

Re: Difference between task and callback? (Task vs Callback)

Post by arnaud »

In FreeRTOS a task is a thread: tasks are running seemingly in parallel and are sharing the same memory. At any time, the task that can run and that has the highest priority, will be the one running on the CPU. On the Crazyflie, as soon as FreeRTOS is launched (this is done very early in the boot), there is only tasks and hardware interrupts running on the CPU.

A callback is simply a function called when an event happened. It is called in the context of a task and it can be very important to know what task it is. This is because, some tasks are time critical and will require the callback to exit quickly.

Generally speaking, a callback should never block. This means that operation like taking a semaphore or sleeping should never be done in a callback. A common pattern is to unlock a semaphore or send a message through a queue in the callback and to do the actual work and sequencing in a task that takes the semaphore or receive from the queue.
Post Reply