Global Vairable handing in FreeRTOS

gauravpatni wrote on Friday, July 21, 2017:

Dear All,

We have just started development based on Free RTOS .But we are unable to find ‘recommended’ way of handling global variable . Please refer following case :

External int is use for counting digital sensor values .

multiple task need to get this digital sensor count for further processing ( read only ) .

As may times i read that use of global vairable is crude method in multitasking system . So request you to please help me by suggesting suitable / recommended method .

Regards,
Gaurav

rtel wrote on Friday, July 21, 2017:

If you have one writer and multiple readers, and the update to the variable is atomic (i.e. the variable is updated in one write, like a 32-bit value being updated in a single 32-bit write as opposed to two 16-bit writes) then an external variable is a very efficient way of doing what you want. You could get issues with multiple writer scenarios though.

gauravpatni wrote on Friday, July 21, 2017:

Thanks for quick reply .

“external variable” means Global variable ?

also please let me know the recommended way for implementing "multiple writer " & " multiple reader " . This will be helpful for future application development .

Regards,
Gaurav

richard_damon wrote on Friday, July 21, 2017:

Gaurav,
When you get into Multiple Writers, Multiple Readers, the big question comes to what do you mean by this and what are behavior are you expecting. At the simple level, it still will just work with a simple atomic int. The variable will have the value last written when every reader reads it. That means if one task writes a value of 5, then another task writes the value 10, after that point all readers will see the 10, and a reader which reads between the two writes will see a 5. If there is no interactions between the writers to synchronize the writes, then it is possible that under other conditions, the 10 might have occurred first, and then the final value would be the 5, so you have some uncertainty due to the ‘race’ condition. Similarly for the readers, if there is nothing synchronizing them to the writers, eventually they will see the later value, but if they happen to read before the last write, they will see the earlier write. Another ‘race’ condition.

Normally, this sort of variability isn’t desired, but there are conditions where it might be acceptable. Thus definition is needed.