How to share variable value from one task to another task

akashmeras wrote on Thursday, May 23, 2019:

Hi,

I would like to use shared variable values form one task to another task for my application.
Can anyone tell me how to Share the variable value from on task to another task in Free RTOS?

i have tried by creating two task and declaring the variable globally and used it between tow task but iam not getting the value created from the task one to the task 2.Can an one help me how to use it…

rtel wrote on Thursday, May 23, 2019:

If the variable can be updated atomically (for example it is not a
32-bit variable on a 16-bit architecture, which would take two writes to
update all 32-bits), and there is only one task that ever writes to the
variable (although many can read from it), then a global variable should
not cause a problem. You don’t provide enough information for me to say
anything more than that though.

akashmeras wrote on Friday, May 24, 2019:

Okay let me give you some simple example regarding my project

I am using two task one will continuously measure the ADC value from the sensor and calculate it to the appropriate value using the formula and the final value will get stored into a global variable(for example the variable is A). Another task purpose is to radically check the value (A) if the value is lesser than 230 it has to do one operation if it is greater than 230 It has to do another operations. for this, I am not able to get the value of A from task one to task two. In my project, there are so many variables and flags which I have used and I have to use that between tasks before Before have done it with normal coding and functions now implementing it with freeRTOS for better efficiency.

richarddamon wrote on Friday, May 24, 2019:

FreeRTOS has nothing to do with getting the value from one part of your code to another via a global variable. FreeRTOS does provide some means to deal with syncronization issues to avoid non-atomic access interaction, or that could be used instead of using a simple global variable.

To send a value, that can be updated atomically (so we don’t need to worry about that issue) my method is as follows.

In a header file, foo.h, I have a statement like:

extern int value;

In the program file foo.c I have a statement, outside of any function, like:

int value;

foo.c, and any other files that want to access value, will include foo.h, and then they can change/read the value as needed, and will be seen in the other files.

DO NOT omit the extern on the decleration in the header, or put the definition in any other files, as that breaks the rules of C in a manner that it is not required to diagnose, and might create multiple variable value that might look to all be the same, but aren’t.

richarddamon wrote on Friday, May 24, 2019:

FreeRTOS has nothing to do with getting the value from one part of your code to another via a global variable. FreeRTOS does provide some means to deal with syncronization issues to avoid non-atomic access interaction, or that could be used instead of using a simple global variable.

To send a value, that can be updated atomically (so we don’t need to worry about that issue) my method is as follows.

In a header file, foo.h, I have a statement like:

extern int value;

In the program file foo.c I have a statement, outside of any function, like:

int value;

foo.c, and any other files that want to access value, will include foo.h, and then they can change/read the value as needed, and will be seen in the other files.

DO NOT omit the extern on the decleration in the header, or put the definition in any other files, as that breaks the rules of C in a manner that it is not required to diagnose, and might create multiple variable value that might look to all be the same, but aren’t.