There are more than 10 tasks in the application I am developing. 9 of the existing tasks modify a global variable (Example: uint8_t DataUnit). The other task uses the modified global variable. But the problem here is that since the tasks work with semaphores, it will be possible to write to the variable at the same time. What method should be followed to avoid this confusion?
It is a bit difficult to say what would be a good implementation without knowing further details: is there literally an array of 100 bytes? Does every task own a couple of entries, or can any task write to any location? Will there be frequent updates of the array? Is it time-critical?
Can you tell what is the meaning of these numbers? Counts?
You can create a mutex that protects the access to the entire array. Access will need 3 steps:
Read or write data
Often when I design an application, I try to make one task responsible for an object. If there is only one task with write-access the status of that object is guaranteed to be “integer”. For the task it is simple and fast to maintain its object(s) when there is no concurrent access.
If other tasks need access to this object, I would let them send messages to the owning task.
But again, it all depends on the type of object. In the above model, I am thinking of for instance a driver for a DSP.
Actually, I want to send data through a task. The size of the data I want to send is not fixed. Because I want to set up a dynamic structure, I defined an array with 100 elements. This array will be global, and after each task changes the content of this array, I want to send it over the communication protocol.
Each task will make changes to the global array and will be sent over the communication protocol. This array will then be cleared.
Note: Sending over the communication protocol is done with another task.
The array will be updated regularly when the relevant conditions are met in the tasks.
To summarize the structure I think in general;
A global array will be defined, each task will synchronize the data it wants to send over the communication protocol to this array after it has fulfilled its own tasks. Afterwards, this global array will be sent to the other system via the task with the communication protocol. Afterwards, the global array will be cleared and other tasks will continue to work in the same way.