I wrote a code that includes global variable, static variable and local variable.
Can you please tell me if I wrote it in the right way ? I mean in the task safe way
u32 GlobVariable = 0;
static u32 SttGlobVar = 0;
static u32 SttLclVar = 0;
u32 locVar = 0;
No, It's RTOS question about using semaphores on static variables.
Anything that is global or static will get referenced by all tasks.
That is, there is only one copy of the variable, so anything that reads
or manipulates the variable will be reading/manipulating the same
variable. In your case you used a semaphore to ensure the only one task
accesses the variables at a time, but you are not checking the return
value of the semaphore before you access the variable. To use a
semaphore you would need to check the 'take' function's return value
then only access the variable if the return value indicated the 'take'
If you are just accessing variables like this then it would be much more
efficient to use a critical section than a semaphore. The critical
region would only be a few assembly instructions.
Thank you for your answer.
I understand that a local variable do get copied for each task and there is no reason to lock read/write operations on a local variable.
Am I right ?
Copyright (C) Amazon Web Services, Inc. or its affiliates. All rights reserved.