Why ensuring mutual exclusion is important when accessing shared resources
A fundamental problem in multitasking is accessing shared resources. Text books often introduce this topic by considering the following problem. Imagine two tasks, both having access to a global variable. The job of one task, called an incrementer, is to increment the shared variable. The other task, called the decrementer, decrements the shared variable. The increment and decrement operations in each task are embedded within identical for
loops. In this way, we arrange for the variable to be incremented and decremented the same number of times. The shared variable is reset to zero before the tasks are created and run. Once the tasks complete, one may expect the value of the shared variable to equal zero, as increment and decrement have been executed in equal measure by the two tasks. This recipe, named RTOS_Sem_c8v0
, illustrates that, surprisingly, this is not the case.
How to do it…
Create a new project and using...