This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

Two tasks calling the same function

Hi All,

This is a general question on TI-RTOS. What will happen if two tasks call the same function? Imagine there are two tasks named "taskPriHi" & "taskPriLo". Now let us assume that there is a function named reverseString(char *str). This function will reverse the string. For example, if str is "abc", then after the function returns, str will be "cba". Now say taskPriLo called reverseString with str as "abc", here str is a local variable of taskPriLo. Now, due to some reason taskPriHi is ready to run & so taskPriLo gets blocked (while reverseString called from taskPriLo didn't return). Now taskPriHi also calls reverseSrting function.

Will reverseString() function called from both taskPriHi & taskPriLo return correct results? How are similar situations handled in an RTOS environment in case of calling standard library functions like strlen(), strstr(), etc?

-

Thanks

-

Regards

Soumyajit

  • Hi Soumyajit,

    Two tasks will have their own stack and as long as the function creates local variables, both tasks will not affect each other.

    However, if there is a global variable in a file that can be accessed by more than one task, then you could end up with unknown results.

    To prevent that, it is recommended to use some sort of locking mechanism (semaphores for example) to protect the critical areas where you want to prevent access to your variable.

    You do have to be careful to avoid deadlocks, where Task 1 would be waiting for Task 2 to release a semaphore and Task 2 would wait for Task to release another semaphore.

    Regards,
    Michel