Ada Concurrency - Avoiding Race Conditions
Race conditions Concurrent tasks are commonly scheduled by the operating system. The result is that the programmer cannot predict the order of execution of tasks. For example, if two tasks increment a shared variable the result may not be what was expected. Num : Integer := 100; -- shared variable Procedure Increment is Begin Num := Num + 1; End Increment; If two tasks simultaneously call the Increment procedure the following interaction is possible Num := 100 + 1; Each task reads the initial value of Num as 100, then adds one to the value and stores 101 into the variable Num. The trouble we see is the Increment function has been executed twice but the value stored in Num is only increased by 1 from the original value. The race condition happens when two or more tasks perform overlapping writes to the same variable. The following program illustrates this problem. with Ada.Text_IO; use Ada.Text_IO; procedure non_deterministic is ...