Use atomic access for flag variables
cc
@mattwala