As you described, the code is not thread-safe, which means you have to use something like a 'lock' to ensure that only one thread runs the piece of code at a time.