Thread safe code implemented using lock