Re: Global Variable Safety with Multi-tasking
- Posted by mattlewis (admin) Aug 14, 2012
- 1541 views
What technique (if any) do I need to use to ensure safety when using global variables with real-time tasks?
For example two real-time tasks need to access a global sequence. Task A will read from configuration file every few minutes and may update (add or remove) some elements in the global sequence. Task B will loop thru the elements in the global sequence performing some operations.
If task B yields to task A and task A changes the number of elements in the sequence, when execution returns to task B that could cause a problem with the "for" loop count?
Do I need to ensure that task B only yields after the for loop is complete to be safe?
If you're going to use a for loop, then yes. The upper bound is calculated when the loop is started, so it wouldn't notice the change in the sequence. Alternatively, you could use something like a while loop, though that would be more complicated, because you'd have to be able to know if your index variable needs to be incremented or decremented depending on how the sequence changed.
It's hard to give more detailed advice without knowing more about what exactly you're trying to do.
Matt