Race conditions exist
Closed this issue · 3 comments
Race conditions exist for mSpeed and the other vehicle data member variables. You need to implement some kind of synchronization scheme to prevent one thread from writing an updated vehicle data member variable while another thread services the timer in ReceiveTimerMethod() and reads those member variables.
Problem was actually a state I hadn't accounted for resulting in a divide by zero. Fixed.
Hey, Dave, the race condition exists because mSpeed
is written in a separate thread from where it is read. Keep in mind that the receive()
method in the listener will be executed on a new thread by the VehicleManager service, while the ReceiveTimerMethod()
is executed on a separate thread. Thus, a race can occur as the thread executing the listener's receive()
method writes mSpeed
while simultaneously the thread executing ReceiveTimerMethod()
reads mSpeed
, leading to a correctness problem.
If I properly understand volatile and synchronized, it should be fixed. I'm not sure how to test it, though.