I did two or more task creation with same task_function and different task_name for xTaskCreate();.
And it seems work OK. But I wonder is it OK or are there any potential bugs.
Each task has its own stack and its own (virtual) set of processor registers - so provided you are not using function scope static variables, what you are doing is fine, and in fact common practice. You can see an example here where three simple LED flash tasks are created from the same function - and the function. The LED to flash could have been passed into the task function using the function parameter: https://sourceforge.net/p/freertos/code/HEAD/tree/trunk/FreeRTOS/Demo/Common/Minimal/flash.c
What if you in fact need to use function scope static variables? I would like to spawn the same task multiple times, but each of the tasks use static variables. When doing a quick test with creating 2 tasks with the same name as above everything works independently except when declaring variables in the task as static (as you indicated). How would we spawn two identical tasks without having to rename the two function 2 different names and having two copies of the same code instead of one copy (if what I’m saying makes sense).
Function scope static variables only have one copy and each task will access the same copy. If that is right for your application, then you can just need to take care of synchronization so that no 2 two task update that variable at the same time. On the other hand, if you are only looking to avoid duplicate code, then you can re-factor common code in a function and call that from multiple task functions.
I am only looking to avoid duplicate code, and to also avoid multiple task functions. One idea was to use a static array instead of a single static variable for each function, and then pass the relevant task to each function call so it knows which static variable to access. I don’t like the fact that future developers would have to be careful using static variables so i’m leaning more toward just duplicating the code and having tasks with different names but the same code (I don’t like it but it seems dummy proof although wasteful)…
The key question is why use static variables in the task function? The DEFINITION of static function variables is that they are shared by all invocations of the function. Use function local variables and you don’t have the sharing issue.
Or make a structure to hold the state with an instance for each task and pass the address of that to the task when you create it.
Yes this makes sense, I was trying to unburden future developers from having to remember the consequence of using a static variable in this scenario, but maybe this is just something programmers should be cognizant of.
As they say, you can’t fix stupid or make things idiot proof (as nature will just make better idiots). ‘Randomly’ making variables static is asking for problems.
My personal preference is making tasks based on a class, and then I have the class member variables to store the state in a way that is easy to share between functions to implement to operation.