vTaskPrioritySet() lets you change a task's priority while your program is running. This helps your program decide which task should run first based on current needs.
vTaskPrioritySet() dynamic priority in FreeRTOS
void vTaskPrioritySet( TaskHandle_t xTask, UBaseType_t uxNewPriority );
xTask is the handle of the task whose priority you want to change.
uxNewPriority is the new priority level (0 is lowest).
vTaskPrioritySet(NULL, 3);xTaskHandle to 5.vTaskPrioritySet(xTaskHandle, 5);This program creates a task with priority 1. Inside the task, it prints its starting priority, then changes its own priority to 3 using vTaskPrioritySet(), and prints the new priority. Finally, the task deletes itself.
#include "FreeRTOS.h" #include "task.h" #include <stdio.h> TaskHandle_t xTask1Handle = NULL; void vTask1(void *pvParameters) { printf("Task1 started with priority %lu\n", (unsigned long)uxTaskPriorityGet(NULL)); vTaskPrioritySet(NULL, 3); // Change own priority to 3 printf("Task1 priority changed to %lu\n", (unsigned long)uxTaskPriorityGet(NULL)); vTaskDelete(NULL); } int main(void) { xTaskCreate(vTask1, "Task1", configMINIMAL_STACK_SIZE, NULL, 1, &xTask1Handle); vTaskStartScheduler(); return 0; }
Changing a task's priority can cause immediate context switches if a higher priority task becomes ready.
Use NULL as the task handle to change the priority of the currently running task.
Priority values start at 0 (lowest) and go up to configMAX_PRIORITIES - 1.
vTaskPrioritySet() changes a task's priority while the program runs.
Use it to make tasks more or less important dynamically.
Remember to pass the task handle and the new priority number.
Practice
What does the FreeRTOS function vTaskPrioritySet() do?
Solution
Step 1: Understand the function purpose
vTaskPrioritySet()is designed to change the priority of an existing task dynamically during runtime.Step 2: Compare with other task functions
Creating, deleting, or suspending tasks are done by different functions, notvTaskPrioritySet().Final Answer:
It changes the priority of a task while the program is running. -> Option AQuick Check:
vTaskPrioritySet() changes priority [OK]
- Confusing vTaskPrioritySet with task creation functions
- Thinking it deletes or suspends tasks
- Assuming priority is fixed at task creation
Which of the following is the correct syntax to change a task's priority using vTaskPrioritySet()?
TaskHandle_t xTaskHandle; // Change priority to 3 _____;
Solution
Step 1: Recall the function signature
The correct function isvTaskPrioritySet(TaskHandle_t xTask, UBaseType_t uxNewPriority), so the task handle comes first, then the new priority.Step 2: Check the options
vTaskPrioritySet(xTaskHandle, 3); matches the correct syntax exactly. Options A, B, and D either swap parameters or use incorrect function names.Final Answer:
vTaskPrioritySet(xTaskHandle, 3); -> Option BQuick Check:
Correct syntax = vTaskPrioritySet(task, priority) [OK]
- Swapping the order of parameters
- Using incorrect function names
- Passing priority before task handle
Consider the following code snippet:
TaskHandle_t xTask1;
// Assume xTask1 is created with priority 2
vTaskPrioritySet(xTask1, 5);
UBaseType_t newPriority = uxTaskPriorityGet(xTask1);
printf("%u", newPriority);What will be printed?
Solution
Step 1: Understand priority change
The callvTaskPrioritySet(xTask1, 5)changes the task's priority from 2 to 5.Step 2: Check the priority retrieval
uxTaskPriorityGet(xTask1)returns the current priority, which is now 5.Final Answer:
5 -> Option CQuick Check:
Priority updated to 5, so output is 5 [OK]
- Assuming priority stays the same after set
- Confusing priority with task handle
- Thinking printf prints task handle
What is wrong with this code snippet?
TaskHandle_t xTask2 = NULL; vTaskPrioritySet(xTask2, 4);
Solution
Step 1: Check the task handle value
The variablexTask2is set to NULL, meaning it does not point to a valid task.Step 2: Understand effect of NULL handle
CallingvTaskPrioritySet()with a NULL handle leads to undefined behavior or runtime error because no valid task exists to change priority.Final Answer:
xTask2 is NULL, so setting priority causes undefined behavior. -> Option DQuick Check:
NULL task handle causes error [OK]
- Ignoring NULL task handle
- Assuming priority range is only 0 or 1
- Confusing handle with pointer to handle
You have two tasks: TaskA with priority 3 and TaskB with priority 5. You want TaskA to temporarily run with higher priority than TaskB during a critical section. Which code snippet correctly achieves this?
TaskHandle_t xTaskA, xTaskB; // Initially priorities: TaskA=3, TaskB=5 // Critical section starts _____; // Critical section ends vTaskPrioritySet(xTaskA, 3);
Solution
Step 1: Identify priority requirements
TaskA must have higher priority than TaskB (which is 5) during the critical section.Step 2: Choose correct priority change
Setting TaskA to 6 makes it higher than TaskB's 5. Options A and D lower TaskB's priority instead. vTaskPrioritySet(xTaskA, 5); sets TaskA to 5, equal to TaskB, not higher.Step 3: Restore original priority after critical section
After the critical section, TaskA's priority is reset to 3 as shown.Final Answer:
vTaskPrioritySet(xTaskA, 6); -> Option AQuick Check:
TaskA priority > TaskB priority during critical section [OK]
- Setting TaskA priority equal to TaskB
- Lowering TaskB priority instead of raising TaskA
- Not restoring original priority after critical section
Ready to prove it?
Pass the test and this pattern moves unknown
